setInterval(function(),time) 在运行时更改时间
setInterval(function(),time) change time on runtime
我想在我的代码运行时更改setInterval函数时间。
我试试这个
<script type="text/javascript">
$(function () {
var timer;
function come() { alert("here"); }
timer = setInterval(come, 0);
clearInterval(timer);
timer = setInterval(come, 10000);
});
</script>
第一次设置间隔不起作用!
您正在清除下一行的间隔,因此第一行不起作用,因为它会立即清除:
timer = setInterval(come, 0);
clearInterval(timer);
timer = setInterval(come, 10000);
此外,正如 gdoron 所说,设置一个无间隔并不是真正有效的,也不是一个真正的好主意,使用 setTimeout 代替,或者在不需要延迟的情况下直接运行函数。
come();
clearInterval(timer);
timer = setInterval(come, 10000);
。您将需要使用 setTimeout,并重复调用它:
var timer; // current timeout id to clear
function come(){ /* do something */};
var time; // dynamic interval
(function repeat() {
come();
timer = setTimeout(repeat, time);
})();
有了这个,您可以设置每次执行函数repeat
时要应用的不同"间隔"。但是,如果在超时期间更改time
,则没有任何变化,您需要为此停止超时。
这是一个旧帖子,但是我已经实现了一个打字稿版本来更改运行时的间隔:
class LoopTimer {
private timer: null | NodeJS.Timer;
private callback: (...args: any[]) => void;
private ms: number;
private started: boolean;
constructor(callback: (...args: any[]) => void, ms: number) {
this.callback = callback;
this.ms = ms;
this.timer = null;
this.started = false;
}
start() {
if (!this.started) {
this.timer = setInterval(this.callback, this.ms);
this.started = true;
}
}
stop() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
this.started = false;
}
}
get getStarted(): boolean {
return this.started;
}
setInterval(ms: number) {
this.ms = ms;
if (this.started) {
this.stop();
this.start();
}
}
}
你可以像这样使用它:当间隔更改时,计时器将停止并重新启动。
const myTimer = new LoopTimer(()=>{
console.log("Hello");
}, 100);
myTimer.setInterval(500);
无法直接更改函数触发的间隔。您能做的最好的事情就是取消一个间隔并设置一个具有相同功能和更新计时器的新间隔。这是一种可能的方法:
timer = {
timers:{},
inc:0,
start:function(cb,gap) {
var key = inc;
inc++;
timer.timers[key] = [setInterval(cb,gap),cb];
return key;
},
stop:function(id) {
if( !timer.timers[id]) return;
clearInterval(timer.timers[id][0]);
delete timer.timers[id];
},
change:function(id,newgap) {
if( !timer.timers[id]) return;
clearInterval(timer.timers[id][0]);
setInterval(timer.timers[id][1],newgap);
}
};
用法:
var myTimer = timer.start(function() {....},1000);
// calls every second
timer.change(myTimer,5000);
// now calls every five seconds
timer = setInterval(come, 0); // zero isn't a valid interval...
您可能希望:
come();
timer = setInterval(come, 10000);
MDN 上的文档:
延迟是 setInterval() 函数在每次调用 func 之前应等待的毫秒数(千分之一秒)。与 setTimeout 一样,强制实施最小延迟。
和:
从历史上看,浏览器实现 setTimeout() "clamping":延迟小于"最小延迟"限制的连续 setTimeout() 调用被强制使用至少最小延迟。DOM_MIN_TIMEOUT_VALUE,最小延迟为 4 毫秒(存储在 Firefox 中的首选项中:dom.min_timeout_value),DOM_CLAMP_TIMEOUT_NESTING_LEVEL为 5 毫秒。
这是MI的例子,我觉得更简单易懂
const timer = {
time: 5, // 5 time in seconds
_time: null,
_timeout: null,
fun: () => {},
start() {
if (this._timeout == null) {
const self = this;
this.fun();
this._timeout = setTimeout(function repeater() {
self.fun();
self._timeout = setTimeout(repeater, 1000 * self.time);
}, 1000 * this.time);
}
},
stop() {
const timeout = this._timeout;
this._timeout = null;
this.set_time(); // set time to default
clearTimeout(timeout);
},
set_time(time) {
if (this._time == null) this._time = this.time;
if (time) {
this.time = time;
} else {
this.time = this._time;
}
},
};
解说:
- time:是每次迭代、周期或下一次调用之间的间隔时间
- _time:此变量保存默认值 time,使用 stop() 时,此变量(_time)恢复"时间"
- start:此函数启动迭代,如果再次调用,则不会重复。 停止
- :此功能,停止超时并设置默认时间和_timeout
- set_time:此函数将新值设置为time,如果不发送参数,则时间恢复为默认值,在此示例中声明运行时为"5"
例:
const timer = {
time: 5, // 5 time in seconds
_time: null,
_timeout: null,
fun: () => {},
start() {
if (this._timeout == null) {
const self = this;
this.fun();
this._timeout = setTimeout(function repeater() {
self.fun();
self._timeout = setTimeout(repeater, 1000 * self.time);
}, 1000 * this.time);
}
},
stop() {
const timeout = this._timeout;
this._timeout = null;
this.set_time(); // set time to default
clearTimeout(timeout);
},
set_time(time) {
if (this._time == null) this._time = this.time;
if (time) {
this.time = time;
} else {
this.time = this._time;
}
},
};
// print time
timer.fun = () =>{
console.log(new Date())
};
timer.set_time(10)
timer.start()
这篇文章很旧,但我需要类似的东西,也许有人需要它。
这是一个没有setInterval的版本,基于来自另一个反应的代码(Niet the Dark Absol)。
function timer()
{
var timer = {
running: false,
iv: 5000,
timeout: false,
cb : function(){},
start : function(cb,iv,sd){
var elm = this;
clearInterval(this.timeout);
this.running = true;
if(cb) this.cb = cb;
if(iv) this.iv = iv;
if(sd) elm.execute(elm);
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);
}
};
return timer;
}
用法:
var timer_1 = new timer();
timer_1.start(function(){
//magic here
}, 2000, false);
var timer_2 = new timer();
timer_2.start(function(){
//more magic here
}, 3000, true);
//change the interval
timer_2.set_interval(4000);
//stop the timer
timer_1.stop();
如果函数需要在 0 处运行,则 start 函数的最后一个参数是布尔值。
您还可以在此处找到脚本:https://github.com/Atticweb/smart-interval
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何根据时间运行不同的脚本
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- http.listen()在运行时接受终端命令
- 自定义运行时Can'在谷歌应用引擎中看不到我的自定义日志
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- JavaScript运行时是如何工作的
- 在运行时创建元素时移到一边时出错
- 如何在运行时在HTML5画布中绘制正方形
- JavaScript 中的创作时间与运行时
- setInterval(function(),time) 在运行时更改时间
- 在 VideoJS 中添加事件侦听器,用于在运行时获取视频持续时间
- 每次 while 循环运行时调用 javascript,以从数据库中给出一个唯一的时间
- 如何在JavaScript中返回以毫秒为单位的运行时持续时间
- 运行时错误&时间复杂性问题:最小化值|(A[0]+..+A[P-1])-(A[P]+..+A[N-1])|
- 使用数据表时,IE8长时间运行脚本错误
- spin.js在IE和FF上长时间运行线程时停止旋转
- 在操作字符串时,获得Javascript长时间运行脚本错误的机会是什么?