Javascript 带延迟的dojo鼠标器
我希望做如下工作:Javascript 带延迟的dojo鼠标器,javascript,dojo,Javascript,Dojo,我希望做如下工作: 当鼠标移到某个元素上时,记录它 如果鼠标停留3秒钟,则对该元素执行一些操作f() 如果鼠标在3秒钟前离开该元素,则不应执行该操作 如何在可能取消的情况下实施延迟执行?使用DOJO库的答案会更好,因为我在项目中使用DOJO工具箱 尝试以下操作: var delay = 3000; dojo.forEach(dojo.query(".some-element-set"), function(element) { dojo.connect(element, "onmo
- 当鼠标移到某个元素上时,记录它
- 如果鼠标停留3秒钟,则对该元素执行一些操作
f()
- 如果鼠标在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);
});
});
有关更多信息,请参阅和文档
编辑:我已根据OP的评论更新了我的答案,请尝试以下操作:
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);
});
});
有关更多信息,请参阅和文档
编辑:我已经根据OP的评论更新了我的答案这看起来很棒。我想我需要一个更广义的版本。我有一组元素(每个元素都有不同的id),我需要用元素的id调用函数f。那会不会太难?这看起来很棒。我想我需要一个更广义的版本。我有一组元素(每个元素都有不同的id),我需要用元素的id调用函数f。那会不会太难?