Joe*_*ano 147 javascript timer setinterval
我编写了一个javascript函数,它使用setInterval在十分之一秒内操作一个字符串,进行一定次数的迭代.
function timer() {
var section = document.getElementById('txt').value;
var len = section.length;
var rands = new Array();
for (i=0; i<len; i++) {
rands.push(Math.floor(Math.random()*len));
};
var counter = 0
var interval = setInterval(function() {
var letters = section.split('');
for (j=0; j < len; j++) {
if (counter < rands[j]) {
letters[j] = Math.floor(Math.random()*9);
};
};
document.getElementById('txt').value = letters.join('');
counter++
if (counter > rands.max()) {
clearInterval(interval);
}
}, 100);
};
Run Code Online (Sandbox Code Playgroud)
我没有将间隔设置为特定的数字,而是希望在每次运行时根据计数器更新它.所以代替:
var interval = setInterval(function() { ... }, 100);
Run Code Online (Sandbox Code Playgroud)
它会是这样的:
var interval = setInterval(function() { ... }, 10*counter);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.似乎"10*counter"等于0.
那么,每次匿名函数运行时如何调整间隔?
nic*_*ick 114
您可以使用匿名函数:
var counter = 10;
var myFunction = function(){
clearInterval(interval);
counter *= 10;
interval = setInterval(myFunction, counter);
}
var interval = setInterval(myFunction, counter);
Run Code Online (Sandbox Code Playgroud)
更新:根据A. Wolff的建议,使用setTimeout以避免需要clearInterval.
var counter = 10;
var myFunction = function() {
counter *= 10;
setTimeout(myFunction, counter);
}
setTimeout(myFunction, counter);
Run Code Online (Sandbox Code Playgroud)
Pet*_*ley 91
请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);
Run Code Online (Sandbox Code Playgroud)
gna*_*arf 24
我喜欢这个问题 - 在我身上启发了一个小计时器对象:
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();
};
Run Code Online (Sandbox Code Playgroud)
使用示例
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);
Run Code Online (Sandbox Code Playgroud)
小智 16
我和原始海报有同样的问题,这是一个解决方案.不确定这是多么有效....
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()
}
Run Code Online (Sandbox Code Playgroud)
小智 8
一种更简单的方法是if在刷新的函数中使用一个语句,并以一个控制来定期执行命令.在以下示例中,我每2秒运行一次警报,并且intrv可以动态更改interval()...
var i=1;
var intrv=2; // << control this variable
var refreshId = setInterval(function() {
if(!(i%intrv)) {
alert('run!');
}
i++;
}, 1000);
Run Code Online (Sandbox Code Playgroud)
这是我这样做的方式,我使用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);
}
};
Run Code Online (Sandbox Code Playgroud)
用法:
timer.start(function(){
console.debug('go');
}, 2000);
timer.set_interval(500);
timer.stop();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135616 次 |
| 最近记录: |