Summary of JavaScript Timer Types

Summary of JavaScript Timer Types

1.setInterval()

Calls the function at a specified interval (in milliseconds).

The method will keep calling the function until clearInterval() is called or the window is closed.

grammar:

setInterval(code,millisec,[arg1, arg2, ...])

parameter describe
code Required. The code string to execute.
millisec must. The time interval in milliseconds.
arg1, arg2, … Optional. When the timer expires, additional parameters will be passed to the function specified by func

2.setTimeout()

Calls a function after the specified number of milliseconds.

grammar:

setTimeout(code,millisec,[arg1, arg2, ...]); //parameters have the same function as above

Final code demonstration:

//1. You can write setTimeout directly using arrow function(()=>{
 this.isSkeleton=false;
  },2000)
 
//2. You can return a value and then call clearTimeout() to cancel the timer;
let a = setTimeout(()=>{
  alert('popup');
  },5000);
function b() {
  window.clearTimeout(a); // Pop-up window appears after 5 seconds, calling b function can directly cancel the pop-up.}

Return value:

The return value intervalID is a non-zero value used to identify the timer created by setInterval() . This value can be used as a parameter of clearInterval() to clear the corresponding timer. Note that setInterval() and setTimeout() share the same ID pool, so avoid mixing them.

Finally, some knowledge:

JS objects can be obtained in two ways: one is customized by the developer; the other is provided by ECMAScript . The objects provided by ECMAScript are called JavaScript built-in objects.

The timer is provided by window object, and window call can also be added before the timer.

This is the end of this article about the summary of JavaScript timer types. For more information about JavaScript timer types, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the JavaScript timer principle
  • Detailed explanation of JavaScript timers
  • JavaScript Timer Details
  • JavaScript timer to achieve limited time flash sale function
  • JavaScript timer to achieve seamless scrolling of pictures

<<:  Solution to PHP not being able to be parsed after nginx installation is complete

>>:  How to solve the problem of not getting form value after submitting html form input using disabled

Recommend

Detailed explanation of how to monitor MySQL statements

Quick Reading Why do we need to monitor SQL state...

Native js realizes the drag and drop of the nine-square grid

Use native JS to write a nine-square grid to achi...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

Tutorial on Migrating Projects from MYSQL to MARIADB

Prepare the database (MySQL). If you already have...

mysql installer web community 5.7.21.0.msi installation graphic tutorial

This article example shares the specific code for...

How to test network speed with JavaScript

Table of contents Preface Summary of the principl...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

How MySQL uses transactions

Basics A transaction is an atomic operation on a ...

UCenter Home site adds statistics code

UCenter Home is an SNS website building system rel...

Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA

1. Add the plug-in and add the following configur...

Vue method to verify whether the username is available

This article example shares the specific code of ...

Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Preface We all know that MySQL query uses the sel...