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 adjust the switching time of js timer

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the js timer how to adjust the switching time of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this js timer how to adjust the switching time article will have a harvest, let's take a look.

1. Timer concept

Timer is also an asynchronous task, usually browsers have an independent timer module, the delay time of the timer is managed by the timer module, when a timer to the executable state, it will be added to the main thread queue.

JS currently has three timers: setTimeout, setInterval and setImmediate.

two。 Start timer

An example of setInterval:

/ / example3.jssetInterval (() = > console.log ('Hello every 3 seconds'), 3000)

This example prints a message every 3 seconds. Executing example3.js with the node command will cause Node to print this message forever until you terminate the process (using CTRL + C).

Because calling the timer function dispatches the operation, you can also cancel the operation before executing it.

3. End timer

Because calling the timer function is a scheduling operation, you can also cancel the scheduling operation before execution.

The call to setTimeout returns a timer "ID", which you can cancel using the timer ID with the clearTimeout call. This is an example:

/ / example4.jsconst timerId = setTimeout (() = > console.log ('You will not see this onetimes'), 0); clearTimeout (timerId)

This simple timer should start immediately after 0 milliseconds, but it doesn't work as we expected, because we have captured the timerId value and canceled it immediately after using the clearTimeout call.

This is the end of the article on "how to adjust the switching time of js timer". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to adjust the switching time of js timer". If you want to learn more, you are welcome to follow the industry information channel.

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: 207

*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

Internet Technology

Wechat

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

12
Report