1
var td = {
setTimeout: function(callback, ms){
var dt = new Date();
    var i = dt.getTime(), future = i+ms;
    while(i<=future){
      if(i === future){
        callback();
      }
      // i = dt.getTime();
      i++;
  }
 }
};
td.setTimeout(function(){
    console.log("Hello"); // executes immediately
}, 3000);

The problem with this approach is that, at the end of the while block I am setting the value of i to i + 1 which I think, should be set to the current timestamp (dt.getTime()), that way, future (a timestamp value at which the callback function should execute) can be compared with the current timestamp, which is the value of i.

But when I comment out i++ and uncomment this line:

i = dt.getTime();

and run the script, the browser freezes. Is there a way to create a setTimout() method that would serve the same purpose as window.setTimeout()?

3
  • 1
    It's is not really possible to simulate setTimeout in user land code because JavaScript doesn't expose any APIs to directly add functions to the event queue. Anyways... what's actually your question? Commented Jun 11, 2017 at 15:54
  • 1
    You should understand that even if you fix the endless loop issue, the browser will still lock up. That's because the loop will prevent anything else from being executed until it terminates. Commented Jun 11, 2017 at 16:05
  • Take a look at this answer of a more-or-less simillar question. Commented Jun 11, 2017 at 16:21

2 Answers 2

1

The problem with your code is, that dt.getTime() always returns the same value (namely the time when the dt object was created. To make this working, you need to do

dt = new Date();

before you set

i = dt.getTime();
Sign up to request clarification or add additional context in comments.

17 Comments

That is exactly what he is doing.
@ibrahimmahrir: no, not in the loop.
One of the reasons why I rather use Date.now()... no reason to hold on to date objects.
@ibrahimmahrir: you are mostly right. But dt.getTime() holds the loop forever! Imagine it returns 5. Then i++ makes it 6. In the next iteration i = dt.getTime() resets it to 5... i never really increases. Getting the current time would at least fix the infinite loop. But as you can see from my comments on the question, I already pointed out that this is not the right approach.
@ibrahimmahrir I imagine everybody encounters something like this at least once when venturing into the mysterious lands of JavaScript ;)
|
0

Behold.. setTimeout that isn't synchronous >:D

function timeout(fn,n){
  (async()=>{
    n=n||0; var time=Date.now()+n
    await new Promise(r=>r()) //asynchronous even if n is 0
    while(Date.now()<time){
      await new Promise(r=>r())
    }
    fn()
  })()
  return true
}

//demonstration
timeout(()=>console.log(3),200)
timeout(()=>console.log(2),20)
console.log(1)

3 Comments

It... doesn't really do a timeout, though. codepen.io/davelnewton/pen/poeqYyK
ty i misplaced a few lines ;-;
Fix "works" in the sense it delays, but as with any busy loop, this will peg the CPU.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.