Setinterval with指数时间减少 [英] Setinterval with exponential time decrease
问题描述
我有一个带有setinterval的mousedown事件。我希望间隔时间可变。所以第一个是500,第二个500/2 = 250,等等。任何提示?
I've got a mousedown event with a setinterval. I want the time of intervals to be variable. So the first one is 500, the second one 500/2 = 250, etc. Any tips?
$plus.mousedown(function(e) {
increment(20)
timeout = setInterval(function(){
increment(20)
}, 500);
});
$(document).mouseup(function(){
clearInterval(timeout);
return false;
});
干杯!
编辑:对不起模棱两可。我想在mousedown期间改变间隔的时间。因此,在执行mousedown时,间隔时间应该改变。因此,不是每次单击鼠标,而是每次连续点击,然后再次重置。
sorry for the ambiguity. I want the time of interval to change during the mousedown. So while the mousedown is being performed the intervaltime should change. So not by every single mouse click but with every continuous click, and then reset it again.
推荐答案
你真的不能做这个用 setInterval()
,除非你继续清除延迟更改,所以你不妨在 setTimeout()$ c $周围写一个包装器c>完成类似的事情:
You can't really do this with setInterval()
unless you keep clearing for a delay change, so you might as well write a wrapper around setTimeout()
to accomplish something similar:
function easingTimeout(delay, fn)
{
var id,
invoker = function() {
fn();
delay = Math.floor(delay / 2);
if (delay) {
id = setTimeout(invoker, delay);
} else {
id = null;
}
}
// start it off
id = setTimeout(invoker, delay);
return {
clear: function() {
if (id) {
clearTimeout(id);
id = null;
}
}
}
使用:
var timeout;
$plus.mousedown(function(e) {
increment(20);
timeout = easingTimeout(500, function() {
increment(20);
});
});
$(document).mouseup(function(){
timeout.clear();
return false;
});
这篇关于Setinterval with指数时间减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!