Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide option for setInterval to execute immediately #20

Closed
kimbauters opened this issue Jan 26, 2014 · 3 comments
Closed

provide option for setInterval to execute immediately #20

kimbauters opened this issue Jan 26, 2014 · 3 comments

Comments

@kimbauters
Copy link
Contributor

The current behaviour for setInterval is to wait the defined time and then execute the function, until the interval is cleared. Since setInterval is the primary function for periodic events, it might be a good idea to add an option to execute the function immediately instead of first waiting for the defined timeout.

For example, a LED could be set to blink using setInterval, where the option would determine if you want the LED to blink immediately or only after the first timeout occurred.

@gfwilliams
Copy link
Member

This should really be a bug report for in Espruino.

But: While I can see it would be useful, I'm not sure:

setInterval(foo, 60000, { execNow : true });

is really any more obvious than:

setInterval(foo, 60000);
foo();

or:

setInterval(foo, 60000);
setTimeout(foo, 1);

I just don't think it's a good idea - mainly because setInterval is something that's used all over the place and is basically a standard (web browsers, node, etc) and changing the behaviour would be bad news.

@kimbauters
Copy link
Contributor Author

Should be in Espruino, again, my apologies.

I leave this one up to you. Another option would be to introduce a new function that just wraps the setInterval and the direct execution in one. I have used this construct a lot so far, so I would argue that it is a fairly typical use case.

@gfwilliams
Copy link
Member

Sorry - I'm going to close this one. Nobody else has had problems with the old implementation, and you could easily just write the following at the top of your code:

function setIntervalNow(a,b,c) {
  a();
  return setInterval(a,b,c);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants