Check if object still exists in JS context?

Jan 12, 2012 at 2:04 PM

I've made a JS Timer object.  Typical usage would be:

[code]

var t = new Timer();
t.tick = function () { /* do stuff */ }
t.interval = 1000;
t.repeat = true;
t.enabled = true;

[/code]

Obviously Timer inherits from ObjectInstance.  Is it possible to discover if the object still exists within the JS context using any of the available functions or properties within ObjectInstance?

The problem I am having is I want to prevent calling the "tick" function if the "t" object has been deleted either via "delete t;" or overwriting it with a fresh "t = new Timer();".

Thank you.

Coordinator
Jan 12, 2012 at 8:44 PM

Nope, there's no way to determine this deterministically.  You can use the same solutions as in regular .NET programming: rely on the user explicitly stopping the timer (t.enabled = false) or override the Finalize() method and perform your cleanup logic once the object gets garbage collected (which of course could be any arbitrary time in the future).

Jan 12, 2012 at 9:47 PM

OK, thanks anyway.  I'll work around it.

Jan 13, 2012 at 10:43 AM
oobenoob: I recommend rather implementing setTimeout and setInterval methods, as that's what used in javascript in general. You can have a look at my implementation here: https://github.com/Alxandr/RunJS/blob/master/src/RunJS.Core/TimeoutHandler.cs