首页 > 其他 > 详细

Intervals and Timeouts

时间:2015-05-19 22:22:34      阅读:140      评论:0      收藏:0      [点我收藏+]

 Intervals

技术分享
 1 var num = 0;
 2 var max = 10;
 3 
 4 function incrementNumber(){
 5   num++;
 6   
 7   // if the max has not been reached, set another timeout
 8   if(num < max){
 9     setTimeout(incrementNumber, 500);
10   } else {
11     alert("Done");
12   }
13 }
14 
15 setTimeout(incrementNumber, 500)
View Code

Timeouts

技术分享
 1 var num = 0;
 2 var max = 10;
 3 
 4 function incrementNumber(){
 5   num++;
 6   
 7   // if the max has not been reached, set another timeout
 8   if(num < max){
 9     setTimeout(incrementNumber, 500);
10   } else {
11     alert("Done");
12   }
13 }
14 
15 setTimeout(incrementNumber, 500)
View Code

 

   Note that when you‘re using timeouts, it is unnecessary to track the timeoutID, because the execution will stop on its own and continue only if another timeout is set. The pattern is considered a best practice for setting intervals without actually using intervals. True intervals are rarely used in production environments because the time between the end of one interval and the beginning of the next is not necessarily guaranteed, and some intervals may be skipped. Using timeouts, as in the preceding example, ensures that can‘t happen. Generally speaking, it‘s best to avoid intervals. 

Intervals and Timeouts

原文:http://www.cnblogs.com/linxd/p/4515676.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!