zoukankan      html  css  js  c++  java
  • Intervals and Timeouts

     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. 

  • 相关阅读:
    Authentication with SignalR and OAuth Bearer Token
    [Web API] 如何让 Web API 统一回传格式以及例外处理[转]
    EF6 Database First (DbContext)
    DbContext运行时动态附加上一个dbset
    命令模式
    责任链模式
    策略模式
    Sql Server isnull() 用法
    状态者模式
    dom元素改变监听
  • 原文地址:https://www.cnblogs.com/linxd/p/4515676.html
Copyright © 2011-2022 走看看