我编写了一个javascript函数,它使用setInterval在十分之一秒内操作一个字符串,进行一定次数的迭代.
function timer() { var section = document.getElementById('txt').value; var len = section.length; var rands = new Array(); for (i=0; irands.max()) { clearInterval(interval); } }, 100); };
我没有将间隔设置为特定的数字,而是希望在每次运行时根据计数器更新它.所以代替:
var interval = setInterval(function() { ... }, 100);
它会是这样的:
var interval = setInterval(function() { ... }, 10*counter);
不幸的是,这不起作用.似乎"10*counter"等于0.
那么,每次匿名函数运行时如何调整间隔?
您可以使用匿名函数:
var counter = 10; var myFunction = function(){ clearInterval(interval); counter *= 10; interval = setInterval(myFunction, counter); } var interval = setInterval(myFunction, counter);
更新:根据A. Wolff的建议,使用setTimeout
以避免需要clearInterval
.
var counter = 10; var myFunction = function() { counter *= 10; setTimeout(myFunction, counter); } setTimeout(myFunction, counter);
请setTimeout()
改用.然后回调将负责触发下一个超时,此时您可以增加或以其他方式操纵时序.
这是一个通用函数,可用于为任何函数调用应用"减速"超时.
function setDeceleratingTimeout(callback, factor, times) { var internalCallback = function(tick, counter) { return function() { if (--tick >= 0) { window.setTimeout(internalCallback, ++counter * factor); callback(); } } }(times, 0); window.setTimeout(internalCallback, factor); }; // console.log() requires firebug setDeceleratingTimeout(function(){ console.log('hi'); }, 10, 10); setDeceleratingTimeout(function(){ console.log('bye'); }, 100, 10);
我喜欢这个问题 - 在我身上启发了一个小计时器对象:
window.setVariableInterval = function(callbackFunc, timing) { var variableInterval = { interval: timing, callback: callbackFunc, stopped: false, runLoop: function() { if (variableInterval.stopped) return; var result = variableInterval.callback.call(variableInterval); if (typeof result == 'number') { if (result === 0) return; variableInterval.interval = result; } variableInterval.loop(); }, stop: function() { this.stopped = true; window.clearTimeout(this.timeout); }, start: function() { this.stopped = false; return this.loop(); }, loop: function() { this.timeout = window.setTimeout(this.runLoop, this.interval); return this; } }; return variableInterval.start(); };
使用示例
var vi = setVariableInterval(function() { // this is the variableInterval - so we can change/get the interval here: var interval = this.interval; // print it for the hell of it console.log(interval); // we can stop ourselves. if (interval>4000) this.stop(); // we could return a new interval after doing something return interval + 100; }, 100); // we can change the interval down here too setTimeout(function() { vi.interval = 3500; }, 1000); // or tell it to start back up in a minute setTimeout(function() { vi.interval = 100; vi.start(); }, 60000);
我和原始海报有同样的问题,这是一个解决方案.不确定这是多么有效....
interval = 5000; // initial condition var run = setInterval(request , interval); // start setInterval as "run" function request() { console.log(interval); // firebug or chrome log clearInterval(run); // stop the setInterval() // dynamically change the run interval if(interval>200 ){ interval = interval*.8; }else{ interval = interval*1.2; } run = setInterval(request, interval); // start the setInterval() }
一种更简单的方法是if
在刷新的函数中使用一个语句,并以一个控制来定期执行命令.在以下示例中,我每2秒运行一次警报,并且intrv
可以动态更改interval()...
var i=1; var intrv=2; // << control this variable var refreshId = setInterval(function() { if(!(i%intrv)) { alert('run!'); } i++; }, 1000);
这是我这样做的方式,我使用setTimeout:
var timer = { running: false, iv: 5000, timeout: false, cb : function(){}, start : function(cb,iv){ var elm = this; clearInterval(this.timeout); this.running = true; if(cb) this.cb = cb; if(iv) this.iv = iv; this.timeout = setTimeout(function(){elm.execute(elm)}, this.iv); }, execute : function(e){ if(!e.running) return false; e.cb(); e.start(); }, stop : function(){ this.running = false; }, set_interval : function(iv){ clearInterval(this.timeout); this.start(false, iv); } };
用法:
timer.start(function(){ console.debug('go'); }, 2000); timer.set_interval(500); timer.stop();