Skip to content

VM : setTimeout is not working in browser runInNewContext #1875

@ahmadalibaloch

Description

@ahmadalibaloch
  • Chrome 70:
  • Platform Ubuntu 18.04:
  • V8:
    I am executing a JS script using VM in browser like this with details below.
    vm.runInNewContext(codeToEval, sandboxObject);

setTimeout, setInterval and other interval built in methods do not work, even if I expose them in sandboxObject.

console.log('start');
setTimeout(()=> {
     console.log('hello timeout');
 }, 2000);
 
 console.log('end');

output:

start
end

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.

console.log('start');
setTimeout(()=> {
     console.log('hello timeout');
 }, 2000).bind(this);
 
 console.log('end');

output:

start
hello timeout
// and an error in console saying setTimeout(...).bind is not a function
// and end is not printed

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions