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:
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:
script language="javascript" >
function show(name)
{alert("Hello World:" name);}
function _show(name)
{
return function()
{
show(name);
}
}
setTimeout(_show(name),1000);
function
Statement: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