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
Add setInterval and clearInterval #29
Comments
Hi Arjix, thanks for the contribution! Agreed that Window.js should also have setInterval and clearInterval. Window.js doesn't preload a Javascript file, other than the native builtins, so this approach doesn't work right now. I don't think we want to add a preloaded Javascript file because it will take a bit longer to load compared to native code, so it would make every startup a bit slower than it could be. Some comments about the code you wrote:
https://windowjs.org/doc/global So you should just do
Clocks for intervals like in setInterval should use a monotonic time source, that only advances forward and always at a constant rate. A good source for that in Javascript is performance.now(): https://developer.mozilla.org/en-US/docs/Web/API/Performance/now So you could just take a timestamp when a callback is registered, and then compare its timestamp to performance.now() when firing callbacks.
-- I guess this change will really have to go in the C++ code. Here's a pointer to the table of setTimeout callbacks if you want to give this a try: Line 173 in 4e50ac3
See also how it's implemented: Line 748 in 4e50ac3
The task_queue is used internally to run Javascript callbacks once on every frame. I think that's the ideal rate to call setInterval: each callback gets called at most once per loop, and as fast as frames are getting pushed to the screen. Let me know if you'd like to make that change, or if someone else should implement this. |
I am not an experienced c++ dev, so I bet if I go ahead and do it myself I will introduce a bug or smth. So yeah, someone else should implement this. |
Sure, np. You can always have your own module with globals and import it manually in your programs:
Just put anything you'd like in the |
Some context to implement this in C++.
To implement setInterval:
Searching with grep for the SetTimeout implementation will find most of the places that need some code (probably js_strings.h is enough, and registering the API in js_api.cc) [1] Line 123 in 4e50ac3
[2] Line 173 in 4e50ac3
[3] Line 748 in 4e50ac3
|
I noticed that setInterval is not a thing in windowjs, so I decided to write my own implementation:
Would be nice if I could contribute that to the repo, but I don't want to mess with c.
The text was updated successfully, but these errors were encountered: