Reputation: 4638
I am not able to access variable inside settimeout function i.e in the following example setTimeout(pop.hide()), 3000); here pop.hide() doesnt work inside settimeout...
Any solutions..
HTML
<div id="logincheck"></div>
JS:
function logClick() {
var sEmail = $('#username').val();
var sPassword = $('#password').val();
var pop = $('#logincheck');
if (($.trim(sEmail).length == 0) && ($.trim(sPassword).length == 0)) {
pop.show();
pop.html('Enter EmailId and Password ');
setTimeout(pop.hide()), 3000);
}
Thanks AB
Upvotes: 0
Views: 1358
Reputation: 1074188
You're calling pop.hide()
and passing its return value into setTimeout
. Exactly the way foo(bar())
calls bar
and passes its return value into foo
. (Or rather, you would be, but you have an extra )
there so it's actually a syntax error — you should be seeing that syntax error in your JavaScript console.)
You're looking for:
setTimeout(function() {
pop.hide();
}, 3000);
There, we define (but don't call) an anonymous function, and pass a reference to that function into setTimeout
. When the timer expires, that function is called, and it calls pop.hide()
.
Or since you're using jQuery, you could use its $.proxy
:
setTimeout($.proxy(pop.hide, pop), 3000);
// or
setTimeout($.proxy(pop, "hide"), 3000);
Or if you have ES5's bind
available (natively or via an ES5 shim):
setTimeout(pop.hide.bind(pop), 3000);
Those both essentially do the same thing as our anonymous function above.
Upvotes: 4
Reputation: 163
The code should be:
setTimeout(function() {
pop.hide();
}, 3000);
Upvotes: 0
Reputation: 2577
Assign to pop in logincheck
var pop = $('#logincheck');
Call set timeout this way
setTimeout(function(){ pop.hide()} , 3000);
Upvotes: 0
Reputation: 7442
you could use the following (anonymous function)
setTimeout(function () {
pop.hide()
}, 3000);
BTW, there is syntax error (an extra )
) in your setTimeout code
Upvotes: 1