Home Web Front-end JS Tutorial How to use setTimeout in JavaScript

How to use setTimeout in JavaScript

Dec 14, 2018 pm 05:08 PM
settimeout

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,.......])
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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);
Copy after login

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>
Copy after login

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 of this Website
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

Top 5 Date Manipulation JS Plugins Top 5 Date Manipulation JS Plugins Feb 28, 2025 am 12:34 AM

Top 5 Date Manipulation JS Plugins

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

See all articles