How to report an error when using jQuery setTimeout to pass string Parameters

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.