It’s there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer
It’s there a way to configure the setInterval method of javascript to execute the
Share
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.
It’s simplest to just call the function yourself directly the first time:
However there are good reasons to avoid
setInterval– in particular in some circumstances a whole load ofsetIntervalevents can arrive immediately after each other without any delay. Another reason is that if you want to stop the loop you have to explicitly callclearIntervalwhich means you have to remember the handle returned from the originalsetIntervalcall.So an alternative method is to have
footrigger itself for subsequent calls usingsetTimeoutinstead:This guarantees that there is at least an interval of
delaybetween calls. It also makes it easier to cancel the loop if required – you just don’t callsetTimeoutwhen your loop termination condition is reached.Better yet, you can wrap that all up in an immediately invoked function expression which creates the function, which then calls itself again as above, and automatically starts the loop:
which defines the function and starts the cycle all in one go.