定时器:times的单位为毫秒。
var timer = setTimeout(fn,times); //在time时间以后执行函数fn.
如果times = 0,则表示,希望fn“尽可能快”的得到执行。
var timer = setInterval(fn,times);
//时间间隔为time,重复执行函数fn.如果fn的执行时间长于时间间隔times,那么接下来的那次将紧接着执行。
因此,如果希望每一次fn执行完了以后,经历时间间隔times都可以再一次执行(有可能被其它情况打断),
可以使用fn函数结尾调用setTimeout函数来实现。
setTimeout(function(){
//processing
setTimeout(arguments.callee, interval);
}, interval);
Timer的一个应用:
var processor = {
timeoutId: null,
//method that actually performs the processing
performProcessing: function(){
//actual processing code
},
//method that is called to initiate processing
process: function(){
//不过有没有执行,把它取消。
clearTimeout(this.timeoutId);
var that = this;
this.timeoutId = setTimeout(function(){
that.performProcessing();
}, 100);
}
};
//try to start processing
processor.process();
在这个模式中,可以保证,即使在100毫秒以内process()被调用了多次,那么performProcessing最终也只会被执行一次。
可以用以下一个精简的函数实现上面的功能:
function throttle(method, context) {
clearTimeout(method.tId);
method.tId= setTimeout(function(){
method.call(context);
}, 100);
}
在IE中,window的resize事件会调用函数多次。我们可以通过throttle函数来消除这种问题。
function resizeDiv(){
var div = document.getElementById("myDiv");
div.style.height = div.offsetWidth + "px";
}
window.onresize = function(){
throttle(resizeDiv);
};
详细解决方案
定时器timer的更上一层楼引用
热度:179 发布时间:2012-11-23 00:03:43.0
相关解决方案
- 吐了,java Timer 终止不了。
- timer.schedule 不能配备按月执行 哪位ggjj帮帮忙
- 吐了,java Timer 停止不了。解决办法
- Registering a Timer Alarm的有关问题
- Java Timer,该怎么处理
- java.util.Timer,该如何处理
- java Timer 定时器不依照时间运行
- Timer 定时器解决思路
- C# Timer 录像时间,该如何解决
- timer 还没有解决,该如何处理
- 关于System.Timers.Timer,该怎么处理
- Timer 类找不到.....为什么?
- Timer 种
- 腾挪的执行者 - ENTER_FRAME & Timer
- 一行读nodejs(二)-控制台和定时器(console &Timer)
- timer.schedule 不能配置按月执行 何位ggjj帮帮忙
- 关于java.util.Timer,该如何处理
- java timer 如何样能够重复利用
- timer.schedule运作一定时间后挂死.
- Timer 怎么让他延迟3秒呢?迷茫.差一句代码.添上去有错
- iPhone/iPad Timer 应用
- 一个android Timer,为啥只执行了一次
- timer()跟execute
- timer()和execute,该如何处理
- 请教怎么才能彻底结束System.Timers.Timer
- system.timers.timer 自动停掉了某个线程解决思路
- 【多线程】Timer 和 ScheduledThreadPoolExecutor
- iOS开发笔记 以 schedcledTimerWithTimerInterval…的方式触发的 timer,在滑动页面上的列表时,timer 会暂停回调,为什么?如何解决?
- JAVA定时任务——Timer
- INFO [Timer-282] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading.....