When you want to call some jQuery code to display a hidden element, and call setTimeout () to set the HTML content after a delay:
The code for the entire page is like this.
Copy codeThe Code is as follows:
<Span style = "font-size: 18px;"> <Head>
<Title> </title>
</Head>
<Body>
<A href = "#" id = 'heihei' onclick = "showNext ('I am veinei')"> show next </a>
<A href = "#" id = "log" style = "display: none"> yes, I am the next </a>
<Script type = "text/javascript" src = "jquery-1.10.2.min.js"> </script>
<Script type = "text/javascript">
Function showNext (text ){
SetTimeout ("$ ('# log'). show (). text (text)", 1000 );
}
</Script>
</Body>
</Html>
</Span>
. Show () is indeed called successfully. But. text () fails to be called. console displays undefined text.
I did not find a better answer to this question... I wonder if jQuery has modified the content of the setTimeout () function, causing variable invalidation.
Then I started the next experiment.
Copy codeThe Code is as follows:
<Span style = "font-size: 18px;"> <Head>
<Title> </title>
</Head>
<Body>
<A href = "#" id = 'heihei' onclick = "showNext ('I am veinei')"> show next </a>
<A href = "#" id = "log" style = "display: none"> yes, I am the next </a>
<Script type = "text/javascript" src = "jquery-1.10.2.min.js"> </script>
<Script type = "text/javascript">
Function showNext (text ){
SetTimeout ("alert (text)", 1000 );
}
</Script>
</Body>
</Html>
</Span>
I want to see if jQuery has a problem. The same error is returned.
Later I looked at the book and found the problem.
When setTimeout () accepts a string parameter, it is executed in the global scope, that is, it is located outside of any function. the simplest solution is to use a local function (anonymous function) to solve this problem.
Copy codeThe Code is as follows:
<Span style = "font-size: 18px;"> <Head>
<Title> </title>
</Head>
<Body>
<A href = "#" id = 'heihei' onclick = "showNext ('I am veinei')"> show next </a>
<A href = "#" id = "log" style = "display: none"> yes, I am the next </a>
<Script type = "text/javascript" src = "jquery-1.10.2.min.js"> </script>
<Script type = "text/javascript">
Function showNext (text ){
SetTimeout (function () {$ ('# log'). show (). text (text);}, 1000 );
}
</Script>
</Body>
</Html>
</Span>
Solved this problem successfully.