Home  >  Article  >  Web Front-end  >  How to use setTimeout in JavaScript

How to use setTimeout in JavaScript

不言
不言Original
2018-12-14 17:08:476207browse

setTimeout can perform a specific process after a certain period of time without repeating it. In this article, we will take a look at how to use the setTimeout timer.

How to use setTimeout in JavaScript

We know that there are two types of timing processors in JavaScript: setInterval and setTimeout. We have introduced setInterval in the previous article Timer, in the following article we will take a look at how to use the setTimeout timer in JavaScript.

setTimeout() is a method belonging to window that is used to call a function or calculate an expression after a specified number of milliseconds.

The basic syntax is as follows

setTimeout(function函数,固定的时间[,参数1,参数2,参数3,.......])

Let’s take a closer look at the usage of setTimeout

Let's first look at a simple code

<!DOCTYPE html>
<html lang = "ja">
  <head>
    <meta charset = "utf-8">
    <title>JavaScript</title>
  </head>
  <body>
    <script>
      var count = 0;
      var countup = function(){
        console.log(count++);
      } 
    </script>
  </body>
</html>

Prepare the count variable, use 0 as the basis, count and add one by one (count), and then use console.log to output. And put this series of processing into the variable of countupp.

Suppose you want to call this variable countup count after 1000 milliseconds, you need to add setTimeout()

The code is as follows

<script>
      var count = 0;
      var countup = function(){
        console.log(count++);
      } 
      setTimeout(countup, 1000);
    </script>

Since setTimeout is only called once, the output 0 and complete after 1000 milliseconds.

How to use setTimeout in JavaScript

#How to use setTimeout() to iterate and count like setInterval()?

We can write setTimeout in the iteration function, that is, {}, and call it with countup();

The code is as follows

<script>
  var count = 0;
  var countup = function(){
    console.log(count++);
    setTimeout(countup, 1000);
  } 
  countup();</script>

Through this To do so, it repeats the same process again for 1000 milliseconds starting from the point once it finished processing.

The operation effect is as follows: processing behavior similar to setInterval.

How to use setTimeout in JavaScript

If we want to stop counting, we need to use clearTimeout.

The code is as follows

var id = setTimeoutl(countup,1000);

By using clearTimeout to specify this id, you can stop the setTimeout processing at any time (obviously, the processing is stopped)

 <script>
  var count = 0;
  var countup = function(){
    console.log(count++);
    var id = setTimeout(countup, 1000);
    if(count > 5){ 
      clearTimeout(id); 
    }
  }
  countup();
</script>

In the above program, when When the count of setTimeout is executed and countup becomes greater than 5 (if (count> 5)), clearTimeout is executed.

Therefore, it can count up to 5.

How to use setTimeout in JavaScript

Finally, let’s take a brief look at the difference between setInterval counting and using setTimeout counting

Using setInterval for iteration processing In the case of: Repeat the same processing after a certain period of time from the processing start point

When iterating setTimeout: Repeat the same processing after a certain period of time from the processing end point

Therefore, even if You specify the time after the same 1000 milliseconds and the time it takes to start the next process will also change.

Additionally, if a process takes longer than the interval, the behavior will be flawed. If you want to ensure there is some margin between processing and processing, we can use setTimeout.

The above is the detailed content of How to use setTimeout in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn