js timer and timer overlay problem

         Wuhan isolated back the next day punch card, Wuhan refueling, against the war and win! Today I want to talk about the issue js simple timer.

         1. the setTimeout delay the implementation time, pay attention to after the specified time only once

    Of course, sometimes we want to make a timer with a delay effect, it has been executed may be, it is to repeatedly call the function itself, as follows: 

      fun();    

      Fun function () {
        the console.log (. 1)
        the setTimeout ( "Fun ()", 1000); // calls itself repeatedly executed
      }

  2. setInterval timer repeated within the specified time intervals, if not clear, would have been executed down

      setInterval(function () {

        console.log(1);

      },1000)

  3. It is noteworthy that in setTimeroutand setIntervaluse, if the incoming parameter, it can only be passed to the function name:

      setInterval(fn,1000)

  4. The following question is, when we are in the process of execution of the timer, you will find an interesting thing, that is cumulative timer, look at the following example: 

    When we repeatedly click on the browser window will find that the print speed faster and faster, which is what we call timers accumulate.    

      were hours;
      document.onclick = function () {
        h = setInterval (function () {
          console.log (1)
        }, 1000)
      }

    So why is there such a situation? Let's give an example, it's like a person every second using a printer, or two points, then the equivalent of two people to use the printer, they are at the same time, so there will be more than the original print every two seconds times faster. So how to solve this problem?

    Timer accumulated problems to resolve: to clear the timer, and then use the timer . Code examples are as follows:

      were hours;
      document.onclick = function () {
        clearInterval (h)
        h = setInterval (function () {
          console.log (1)
        }, 1000)
      }

    

 

Guess you like

Origin www.cnblogs.com/chysunny/p/12609844.html