I want to run my function at the first time immediately (without timeout) so I do this:
setInterval(function(){
alert("Boo");
}(), 1000);
The function executed at first time but in next intervals, nothing happened. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The better question is, what are you actually trying to achieve ?
You don’t
returnanything from the self-invoking function, so it will return theundefinedvalue implicitly, which is passed over tosetTimeout. After the initial call, the line would look likewhich obviously, makes no sense and won’t continue calling anything.
You either need to to
returnanother function from your self-invoking function, or just don’t use a self-invoking function and just pass over the function reference.Update:
You updated your question. If you want to run a function “once” on init and after that use it for the interval, you could do something like