Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the setInterval () method in JavaScript

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use the setInterval() method in JavaScript, I believe most people still do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

JavaScript setInterval() method operation practice

The setInterval() method repeats the given function every given time interval.

window.setInterval(function, milliseconds);

The window.setInterval() method can be written without the window prefix.

The first parameter is the function to be executed.

The second parameter is the length of the time interval between each execution.

This example executes the function "myTimer" once per second (like a digital watch).

examples

Display current time:

var myVar = setInterval(myTimer, 1000);

function myTimer() {

var d = new Date();

document.getElementById("demo")[xss_clean] = d.toLocaleTimeString();

}

There are 1000 milliseconds in a second.

How to stop execution?

The clearInterval() method stops execution of the function specified in the setInterval() method.

window.clearInterval(timerVariable)

The window.clearInterval() method can be written without the window prefix.

The clearInterval() method uses variables returned from setInterval():

myVar = setInterval(function, milliseconds);

clearInterval(myVar);

examples

Similar to the previous example, but we added a "Stop Time" button:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report