Forum Moderators: open

Message Too Old, No Replies

calling setTimeout from within a method

"Can't execute code from a freed script"

         

bryndyment

6:45 am on May 8, 2003 (gmt 0)

10+ Year Member



Heya,

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?

le_gber

8:43 am on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's working for me with IE6, Opera 6, Netscape 4.78 on WinXP

Leo

BlobFisk

9:47 am on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm unable to recreate this either. Is your script in an external file? Is there something in your system settings that may be causing this error?

bryndyment

1:55 pm on May 8, 2003 (gmt 0)

10+ Year Member



Sorry... yes, these methods are in an external file, included via <script src="abc.js" type="text/javascript">.

(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').

BlobFisk

2:02 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you post a little more of the code? It may help us to find a solution.

bryndyment

3:47 pm on May 8, 2003 (gmt 0)

10+ Year Member



OK, here's code to recreate the problem:

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.

bryndyment

4:02 pm on May 8, 2003 (gmt 0)

10+ Year Member



OK, if I replace "this.methodTwo()" with "thingie.methodTwo()", then it works.

However, I assume this is bad design (referencing "thingie" from within one of its own methods). Should I try to pass "thingie" in as a parameter to methodOne()?

korkus2000

5:30 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think your problem is you are passing a string instead of an object reference. I don't think it understands this.methodTwo() but does understand the complete object method call. It is probably trying to execute this as an object. I don't really see a problem with executing the method with a direct reference to the object. Are you prototyping a custom object or a normal object?

<added>Just saw the bottom part where you create the object. I am guessing it is a custom object.</added>

bryndyment

5:52 pm on May 8, 2003 (gmt 0)

10+ Year Member



"thingie.methodTwo()" works because setTimeout needs that function call to be valid in a global context. "this.methodTwo()" fails because "this" is no longer valid when the timer elapses.

bryndyment

8:02 pm on May 8, 2003 (gmt 0)

10+ Year Member



I just tried, but passing a function reference instead of a string for the function (setTimeout() allows both... I didn't realize this).

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.