0
<script>
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function customFunc(argument)
{
        sleep(1000);
    console.log("Inside third party custom function for event = " + argument.event);
}

var aO = {
       tt:{ 
    cf : customFunc
}};
</script>

I have written a custom sleep function in Javascript which would sleep for the milliseconds it takes as an argumentin sleep function and I have called it in customFunc in which I want to create a delay.Is this the right way or is there any other way I can do it better.

7
  • 7
    I would recommend not doing this at all, and instead using setTimeout. Busy waiting isn't a good idea. Commented Nov 5, 2013 at 17:00
  • @Chad so something like function customFunc(argument) { sleep(1000); console.log("Inside third party custom function for event = " + argument.event); } setTimeout(customFunc,1000); Commented Nov 5, 2013 at 17:01
  • i would use a self-closing showModalDialog() to pause without hammering the CPU. Commented Nov 5, 2013 at 17:05
  • @dandavis can you elaborate in terms of code. Commented Nov 5, 2013 at 17:06
  • 1
    you make an html file with a script that goes setTimeout("self.close()", 3000), and three seconds after your showModalDialog("myfile.html") call, the line under it will fire. Commented Nov 5, 2013 at 17:08

1 Answer 1

2

Very clever -- but I think in most situations you'll be served better by JavaScript's built-in functions:

Sign up to request clarification or add additional context in comments.

1 Comment

@macke: Interesting resource, with some good points about W3Schools. I've added MDN links above (but left the originals in place in case the original poster has already referred to them).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.