I have the following JS:
function TrackTime() {
this.CountBack = function(secs) {
setTimeout(function(){this.CountBack(secs)}, SetTimeOutPeriod);
}
}
I have tried this with a closure (seen above) and also about a dozen other ways. I can't seem to get this to work in any browser. The setTimeout function works fine when not being called in a "class" function. Can someone please help me with this?
This happens because of the change in the scope of "this" when the function is executed.
Try that-this trick..
function TrackTime() {
this.CountBack = function(secs) {
var that = this;
setTimeout(function(){that.CountBack(secs)}, SetTimeOutPeriod);
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With