![]() | 1 ahxxm 2013-08-07 10:42:42 +08:00 |
![]() | 2 jjplay 2013-08-07 10:43:27 +08:00 clearTimeout 是爷 clearInterval 是 interval 的爹 setTimeout setInterval 都是clearTimeout的孙 |
![]() | 3 darasion 2013-08-07 10:56:09 +08:00 一般不用setInterval 比如,发送异步请求时,如果响应很慢的话,这时用setInterval有可能堆积很多请求。 |
![]() | 4 lichao 2013-08-07 11:00:37 +08:00 3 楼正解,应该优先使用 setTimeout。如果是比较耗时的任务,setInterval 会造成任务堆积。 |
![]() | 6 Hyperion 2013-08-07 12:38:08 +08:00 这些函数都木有在标准里出现, 所以都是各家浏览器自行实现的. 但, 可以乱用不代表推荐乱用啊... 比如html5标准里出现这个, 如果以后浏览器规定死不能互换, 你怎么办? setInterval 就是个坑, 内存泄漏的源泉... |
![]() | 7 juicy 2013-08-07 12:48:27 +08:00 |
![]() | 10 Niris 2013-08-07 14:26:19 +08:00 |
![]() | 11 lichao 2013-08-07 14:47:57 +08:00 |
![]() | 13 Niris 2013-08-07 16:35:00 +08:00 The browser will not queue up more than one instance of a specific interval handler. -- Secrets of the Javascript Ninja When using setInterval(), timer code is added to the queue only if there are no other instances of the timer code already in the queue. --PROFESSIONAL Javascript 两本书都这么说的。 可以试试下面的代码输出几个 interval var i = window.setInterval(function() { console.log('interval', Date.now()); }, 1000); window.setTimeout(function() { window.clearInterval(id); console.log('stop', Date.now()); }, 5900); var t = Date.now() + 5000; while(Date.now() < t) { console.log(1); } |
![]() | 14 davepkxxx 2013-08-07 16:55:27 +08:00 setInterval is evil. |