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

What is the third parameter of setTimeout and how to use it

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

Share

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

Today, Xiaobian will share with you what the third parameter of setTimeout is and how to use it. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you will gain something after reading this article. Let's find out together.

Let's start with a simple code:

setTimeout(function(x) { console.log(x);}, 1000, 1);

Console output 1, then can you continue to add parameters? Let's continue with the following code:

setTimeout(function(x,y) { console.log(x+y);}, 1000, 1, 2);

The console outputs 3, which is obviously the sum of the third and fourth arguments.

See here many small partners should realize, yes, the third parameter of setTimeout is the parameter to the first function of setTimeout.

By querying MDN we can see the description of the third parameter:

var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]); var timeoutID = scope.setTimeout(function[, delay]);var timeoutID = scope.setTimeout(code[, delay]);

Therefore, in fact, setTimeout can have countless parameters, but from the third parameter, it is an optional parameter.

All right, what problem can we solve with this feature? One of the most classic is the use of setTimeout in a for loop.

for(var i = 0; i

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