怎么可能使用this
代替setInterval
and setTimeout
calls?
我想这样使用它:
function myObj() {
this.func = function(args) {
setTimeout(function() {
this.func(args);
}, 1000);
}
}
前段时间我这样做是为了.onclick
事件是这样的:
this.click = function(func) {
this.elem.onclick = (function(func, obj) {return function(){func.apply(obj)} })(func,this);
};
但我不知道我该怎么做intervals
and timeouts
.
最简单的方法就是保存this
变成当地人。本地的self
不因上下文而改变setInterval
and setTimeout
回调被调用。相反,它将保留原来的this
value
function myObj() {
var self = this;
this.func = function(args) {
setTimeout(function() {
self.func(args);
}, 1000);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)