我希望做如下的事情:
f()
如何通过可能的取消实现此延迟执行?使用DOJO库的答案会更好,因为我在我的项目中使用DOJO工具包。
答案 0 :(得分:3)
尝试以下方法:
var delay = 3000;
dojo.forEach(dojo.query(".some-element-set"), function(element) {
dojo.connect(element, "onmouseover", function() {
// dojo.partial(f, this.id) will apply `this.id` to `f`, but it
// will not execute it and will only a new function
this._timer = setTimeout(dojo.partial(f, this.id), delay);
});
dojo.connect(element, "onmouseout", function() {
// this._timer was set as an element attribute in the above event
// handler so we don't have to keep track of them separately in
// some silly array/object
clearTimeout(this._timer);
});
});
有关详细信息,请参阅query,forEach,connect和partial文档。
编辑:我根据OP的评论更新了我的答案