-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
setTimeout is not working #24
Comments
yeah, this is because vm-browserify creates an iframe to run the code in, and immediately destroys it after running the code. so if you schedule async callbacks, they will not get called. it would be nice to find a solution for this, i'm not sure how to do it because we don't want to keep the iframes around forever. |
But why does it work when I write .bind(this), and script stops working at that line too, because of the errro |
setTimeout doesn't return anything, so you can't call methods on it. setTimeout itself is synchronous, so the code following it executes immediately. This is true in both cases. The .bind call throws an error synchronously *after* the callback is scheduled. So in both cases a callback is scheduled but never run because the iframe is destroyed
…On April 20, 2019 5:53:21 PM GMT+02:00, Ahmad Ali ***@***.***> wrote:
But why does it work when I write .bind(this), and script stops working
at that line too, because of the errro `.bind is not a function`.
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#24 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
The issue is same for builtin This issue is also discussed here, any workarounds are appreciated. |
The setTimeout(
function () {
// ...
}.bind(this)
) But it's just not possible to use setTimeout in vm-browserify. async code doesn't work, because we destroy the iframe immediately. |
If you say that the iFrame is destroyed in which the code was running then how my custom implementation of setTimeout and setInterval is working.
Even if I add
|
It's because there are multiple windows at play. If you call If you call your custom setTimeout, or pass the vm.runInNewContext('code', { setTimeout: window.setTimeout }) this calls out to the parent window; so in fact your callback is scheduled in the parent window, not inside the iframe. Then if the iframe is destroyed the callback still lingers and will be called by the parent window. |
I tried checking your suggestion |
I ended up creating a custom setTimeout, setInterval, clearTimeout, clearInterval, explained in the link below. And it plays very well! |
I've found a faster solution: vm.runInNewContext('code', {
setTimeout: (callback, wait) => setTimeout(() => callback(), wait),
clearTimeout: handle => clearTimeout(handle)
}) |
setTimeout, setInterval and other interval built in methods do not work, even if I expose them in context.
output:
It is to be noted that when I add
.bind(this)
, then timeout works and breaks at that line saying .bind is not a function.output:
The text was updated successfully, but these errors were encountered: