Reputation: 1417
here my function with setTimout call just immedieatly after the click but i wants it to fire ater 10 sec of click
$('#switchsubjects').click(function(e)
{
i++;
var x=$('#subject-class-switch').html();
if(i<2)
{
$(this).append('<ul id="dcl"><li class="ui-btn-inner ui-li">Class</li><li class="ui-btn-inner ui-li">Subjects</li></ul>');
e.stopPropagation();
setTimeout(rm(),10000);
}
else
{
$('#dcl').remove();
i=0;
}
});
function rm()
{
console.log("working");
$('#dcl').remove();
i=0;
}
Upvotes: 0
Views: 60
Reputation: 24350
setTimeout("rm()",10000);
Otherwise rm()
is executed immediatly, and only its results is evaluated 10s later.
Upvotes: 0
Reputation: 16584
You didn't understand the difference between a function call and a function reference. Let's say you have this function
function myFunction() {
// some code
}
Then this is an immediate function call
myFunction()
And this is a function reference
myFunction
setTimeout's first parameter has to be a function reference (or a string containing javascript code, but this is deprecated).
setTimeout(rm, 10000);
Upvotes: 1
Reputation: 150080
Try this:
setTimeout(rm ,10000); // note no parentheses after rm
The setTimeout()
function expects the first parameter to be a function reference (or a string, but a string is almost always the wrong choice). By saying rm()
you were actually calling the rm
function at that point and passing its return value to setTimeout()
(in this case the return value is undefined
). If you pass rm
without the parentheses that passes a reference to your function that setTimeout
can then call after the delay you specify.
The doco at MDN is pretty good for more information and other examples.
Upvotes: 1