function myfunction()
{
window.setTimeout(alert('I waited for you.'),700000000);
}
I call a function like this and I get an immediate alert saying “I waited for you”.
I initially put in 7000 as the second argument, but kept pushing it up in case it was something besides seconds. I obviously want the function to do something else, but I broke it down to this simplistic example to prove to myself where the problem lies.
What’s my mistake?
You have to pass a function reference, not the result of executing
alert()like this:When you pass
alert('I waited for you.')tosetTimeout(), you’re telling the javascript interpreter to executealert('I waited for you.')and then pass the return result tosetTimeout(). Since that immediately executes thealert()statement and alert doesn’t return a function forsetTimeout()to use, that is clearly not what you want.Instead, you want to pass a function reference to
setTimeout(). That can be done either with an anonymous function as in the example I provided above or it can be done with a separate named function like this:Note: for more advanced usages, you can actually pass an immediately executed function to
setTimeout()as long as that function returns a function reference thatsetTimeout()can call later. But, that is clearly not the usage you are attempting here with youralert()