Home > Web Front-end > JS Tutorial > Discussion on the parameter passing problem of SetTimeOut in JQuery_jquery

Discussion on the parameter passing problem of SetTimeOut in JQuery_jquery

WBOY
Release: 2016-05-16 17:34:07
Original
1264 people have browsed it

Whether it is window.setTimeout or window.setInterval, you cannot take parameters when using the function name as the calling handle. If you want to pass in parameters, whether it is custom parameters or event parameters, the solution is to create a layer of encapsulation based on this function. The specific principle is not clear yet, but the method below can indeed solve this problem.
Look at a simple code first:

Copy the code The code is as follows:

function show(){
alert("Hello World");
}
setTimeout(show,1000);

The effect of this code is Hello world will be displayed after 1 second, but if it is changed to
setTimeOut(show(),1000);
it will be displayed immediately and the delay effect will not be achieved. But it's ok if you add quotes. For example:
setTimeOut("show()",1000);
is fine. But if you bring parameters, it still won’t work. For example:
setTimeOut("show(name)",1000)
At this time, there is a comparison method, which is to write another function, which returns a function without parameters. The function is as follows:
Copy code The code is as follows:

script language="javascript" >
function show(name)
{alert("Hello World:" name);}
function _show(name)
{
return function()
{
show(name);
}
}
setTimeout(_show(name),1000);
function

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template