You’re calling some jQuery code to show a hidden element and set its HTML content
after a time delay using setTimeout():
The .show() call works, but the .html(text) call fails. The Firebug console reports that the text variable is undefined. The same jQuery code works when you don’t call it from setTimeout(). Is there a problem using jQuery with setTimeout()?
When we try this version of the code, the same problem occurs: there is no alert, and Firebug again reports that text is undefined.
This doesn’t identify the problem, but it narrows it down a lot. It clearly isn’t jQuery (unless the mere presence of the jQuery library is interfering with your page, but you can rule that out by running the code in a simple test page that doesn’t include jQuery).So, it must be something wrong with this code itself, most likely to do with the way we’re using setTimeout().
Indeed, the problem here is that when a string argument is passed to setTimeout(), it is executed in the global scope, i.e., as if the code were located outside of any function.The easiest way to fix it is to use a local function for the callback instead of a text string:
Unlike code in a string, a nested function has full access to the outer function’s variables and parameters. So, this code will alert the text as expected.
And finally, here is a corrected version of the original jQuery code:
J Query Related Interview Questions
|HTML 4 Interview Questions||HTML Interview Questions|
|HTML 5 Interview Questions||Zend Interview Questions|
|JqueryUI Interview Questions||Dynamic HTML Interview Questions|
|XQuery Interview Questions||jQuery Mobile Interview Questions|
J Query Related Practice Tests
|HTML 4 Practice Tests||HTML Practice Tests|
|HTML 5 Practice Tests||Zend Practice Tests|
|PHP and Jquery Practice Tests||J Query Practice Tests|
|CodeIgniter Practice Tests||Dynamic HTML Practice Tests|
J Query Tutorial
Selecting Elements With Jquery
Beyond The Basics
Faster, Simpler, More Fun
Html Form Enhancements From Scratch
Html Form Enhancements With Plugins
Interface Components From Scratch
User Interfaces With Jquery Ui
Jquery Ui Theming
Jquery, Ajax, Data Formats: Html, Xml, Json, Jsonp
Using Jquery In Large Projects
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.