question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setTimeout is not working

See original GitHub issue

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

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ahmadalibalochcommented, Apr 23, 2019

I tried checking your suggestion vm.runInNewContext('code', { setTimeout: window.setTimeout }) but it doesn’t work. Custom setTimeout do work.

1reaction
ahmadalibalochcommented, Apr 23, 2019

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.

const setTimeouts = [];
function customSetTimeout(cb, interval) {
  const now = window.performance.now();
  const index = setTimeouts.length;
  setTimeouts[index] = () => {
    cb();
  };
  setTimeouts[index].active = true;
  const handleMessage = (evt) => {
    if (evt.data === index) {
      if (window.performance.now() - now >= interval) {
        window.removeEventListener('message', handleMessage);
        if (setTimeouts[index].active) {
          setTimeouts[index]();
        }
      } else {
        window.postMessage(index, '*');
      }
    }
  };
  window.addEventListener('message', handleMessage);
  window.postMessage(index, '*');
  return index;
}

function customClearTimeout(setTimeoutId) {
  if (setTimeouts[setTimeoutId]) {
    setTimeouts[setTimeoutId].active = false;
  }
}

Even if I add customSetTimeout for 1 minute, it works

 const codeToEval = `console.log('start)
 var a = setTimeout(function() {
    console.log('setTimeout  async done');
 }, 10000);
 console.log('sync');`
 const sandbox = {
      setTimeout: customSetTimeout,
      clearTimeout: customClearTimeout,
    };
vm.runInNewContext(codeToEval, sandbox);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is JavaScript's Set Timeout not working? - Stack Overflow
7 Answers 7 · Remove the parenthesis in setTimeout(startTimer(),startInterval); . Keeping the parentheses invokes the function immediately. · Your startTimer ...
Read more >
Fix JavaScript setTimeout not working - Weekend Projects
This is due to when a function is executed as a parameter to setTimeout , the execution context is different to the execution...
Read more >
setTimeout() - Web APIs | MDN
setTimeout () is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack. In ......
Read more >
Javascript SetTimeout delay not working when trying to ...
I'm trying to implement automatic logout for mobile app for an N minutes. I used javascript node to call setTimeout() javascript function. I've...
Read more >
Javascript bug: setTimeout is not always working - SitePoint
setTimeout (function () {try_conn (data.idPeer, stream); }, connect_time);. it works 70% of the time. But in about 30% of times it does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found