Reputation: 169
I'm using the code below in on of my javascript functions. I want to call this function after 10 seconds. However the function is fired right away!?! Not sure what's going on.
<script>
function testing() {
//other stuff
setTimeout(testing2('value'), 10000);
}
function testing2(value) {
//other stuff
}
</script>
Upvotes: 0
Views: 2185
Reputation: 21130
The problem is that you're passing the value returned from testing2('value')
rather than a function into setTimeout
.
Try this.
setTimeout(function() {
testing2('value');
}, 10000);
Upvotes: 1
Reputation: 150253
You need to pass a function as an argument, not call the function.
setTimeout(function(){
// Inside the callback we do what we want.
testing2('value');
}, 10000);
Upvotes: 1
Reputation: 11958
testing2
is called immediately because your wrote it with argument.
setTimeout
needs a function as first parameter.
And if you write testing2('value')
javascript of course have to execute your testing2
at first to get the result and only after that pass the result to setTimeout
Upvotes: 2