Forum Moderators: open
I have a method that wants to call 'setTimeout', but I don't know how to specify another method as the callback function.
So, I want something like this:
function methodOne() {
setTimeout( 'this.methodTwo()', 5000 );
}
function methodTwo() {
alert( 'success!' );
}
I get an error "Can't execute code from a freed script", which I think is really unfair.
Any ideas, smart people?
(I'm not doing anything like change pages while the timer is active, though. It works fine if I replace 'methodTwo()' with 'alert()' as the first parameter, for instance.)
Also, to clarify, these are methods (defined via '.prototype.methodName').
thingie.js:
---
function _thingie() {
}function _methodOne() {
alert( '1' );
setTimeout( 'this.methodTwo()', 5000 );
}
function _methodTwo() {
alert( '2' );
}
_thingie.prototype.methodOne = _methodOne;
_thingie.prototype.methodTwo = _methodTwo;
thingie.htm:
---
<script src="thingie.js"></script>
<script>
var thingie = new _thingie();
onload = function() {
setTimeout( 'thingie.methodOne()', 5000 );
}
</script>
I'm now getting "Object doesn't support this property or method (thingie.htm, line 1, char 1)", five seconds after the first alert is displayed. Different error, but probably (?) the same root cause.
<added>Just saw the bottom part where you create the object. I am guessing it is a custom object.</added>
The good news is that it does successfully call my callback, but, unfortunately, it's not a "method" call (i.e., any attempts to access "this.myStuff" fails).
So, I think the only way to do it is with the function name embedded in the string.