我有以下JS代码:
function TrackTime() {
this.CountBack = function(secs) {
setTimeout(function(){this.CountBack(secs)}, SetTimeOutPeriod);
}
}
我已经尝试过使用闭包(如上所示)以及其他十几种方法。但似乎在任何浏览器中都无法使其正常工作。当setTimeout函数不在“类”函数中调用时,它可以正常工作。请问有人能帮助我吗?
我有以下JS代码:
function TrackTime() {
this.CountBack = function(secs) {
setTimeout(function(){this.CountBack(secs)}, SetTimeOutPeriod);
}
}
我已经尝试过使用闭包(如上所示)以及其他十几种方法。但似乎在任何浏览器中都无法使其正常工作。当setTimeout函数不在“类”函数中调用时,它可以正常工作。请问有人能帮助我吗?
function TrackTime() {
this.CountBack = function(secs) {
var that = this;
setTimeout(function(){that.CountBack(secs)}, SetTimeOutPeriod);
};
}
var that = this;
this.CountBack = function (secs) {
setTimeout(function () {that.CountBack(secs)}, SetTimeOutPeriod);
}
// This will call a function using a reference with predefined arguments.
function partial(func, context /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments, 2);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(context ? context : this, allArguments);
};
}