SetTimeout actually has a third parameter?
setTimeout actually has a third parameter?
Speaking of setTimeout, everyone is familiar with it, and its usage is very simple: setTimeout(fun, delay).
But you may not believe it, but setTimeout, which has been used for so many years, actually has a third parameter. Let’s take a look at the third parameter of setTimeout.
[Related course recommendations: JavaScript video tutorial]
Let’s look at a simple code first:
setTimeout(function(x) { console.log(x); }, 1000, 1);
The console outputs 1, then it can Why can't I continue to add parameters? Let's continue to look at the following code:
setTimeout(function(x,y) { console.log(x+y); }, 1000, 1, 2);
The console output is 3, which is obviously the sum of the third and fourth parameters.
After seeing this, many friends should realize that, yes, the third parameter of setTimeout is the parameter of the first function of setTimeout.
By querying MDN https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout 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]);
So, to be precise, setTimeout can have countless parameters, but starting from the third parameter, it is optional parameters.
Okay, what problems can we solve after knowing this feature? The most classic one is to use setTimeout within a for loop.
for(var i = 0; i<6; i++) { setTimeout(function() { console.log(i); }, 1000); }
The above example is a classic interview question. It will output 6 consecutively 6 times because setTimeout is an asynchronous operation, and by the time setTimeout is executed, the for loop has been executed, and i has been is equal to 6, so 6 times 6 is output. I have summarized several solutions. Interested friends can read my last blog "About using setTimeout in for loops". At the end of this blog, I mentioned using the third parameter of setTimeout. Let's come back. Check out this approach.
for(var i=0;i<6;i++) { setTimeout(function(j) { console.log(j); }, 1000, i); }
Since each parameter passed in is the value taken from the for loop, 0~5 will be output in sequence. Of course this is still a scope issue, but here the third parameter of setTimeout saves the value of i. This solution is much lighter than using closures.
In addition, the third parameter can also be used as a function.
for(var i=0;i<6;i++) { setTimeout(function(j) { console.log(j); }, 1000, i); }
The final output is 0 for the first time and 1 for the second time.
You can see that the third parameter can also be executed first, and then the function is executed.
Finally, one thing you need to pay attention to when using the third parameter is the compatibility issue. If you need to be compatible with IE9 and previous versions, you need to introduce a piece of code provided by MDN that is compatible with old IE https://developer.mozilla .org/zh-CN/docs/Web/API/Window/setTimeout#Compatible with old environment (polyfill), the portal is posted here, if you are interested, you can take a look.
The following is a description of compatibility on MDN:
Note: Passing additional arguments to the function in the first syntax does not work in Internet Explorer 9 and below. If you want to enable this functionality on that browser, you must use a polyfill (see the Polyfill section).
This article comes from the js tutorial column, welcome to learn!
The above is the detailed content of SetTimeout actually has a third parameter?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



New feature of PHP5.4 version: How to use callable type hint parameters to accept callable functions or methods Introduction: PHP5.4 version introduces a very convenient new feature - you can use callable type hint parameters to accept callable functions or methods . This new feature allows functions and methods to directly specify the corresponding callable parameters without additional checks and conversions. In this article, we will introduce the use of callable type hints and provide some code examples,

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

During the development process, we may encounter such an error message: PHPWarning: in_array()expectsparameter. This error message will appear when using the in_array() function. It may be caused by incorrect parameter passing of the function. Let’s take a look at the solution to this error message. First, you need to clarify the role of the in_array() function: check whether a value exists in the array. The prototype of this function is: in_a

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores

C++ parameter type safety checking ensures that functions only accept values of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

Although large-scale language models (LLM) have strong performance, the number of parameters can easily reach hundreds of billions, and the demand for computing equipment and memory is so large that ordinary companies cannot afford it. Quantization is a common compression operation that sacrifices some model performance in exchange for faster inference speed and less memory requirements by reducing the accuracy of model weights (such as 32 bit to 8 bit). But for LLMs with more than 100 billion parameters, existing compression methods cannot maintain the accuracy of the model, nor can they run efficiently on hardware. Recently, researchers from MIT and NVIDIA jointly proposed a general-purpose post-training quantization (GPQ).

The difference between settimeout and setInterval: 1. Trigger time, settimeout is one-time, it executes the function once after setting the delay time, while setinterval is repetitive, it will execute the function repeatedly at the set time interval; 2. Execution times, settimeout is only executed once, and setinterval will be executed repeatedly until canceled.
