tags:

views:

269

answers:

6

is there a function that would add a time delay between 2 lines of code. Not the settimeout, because settimeout requires a function/object in its arguments.

I am looking for something like this pseudo code

write "abc";
delay(500);
write "xyz";

TIA

Edit: jimr's solution in my other thread worked for my purpose, and so is Robusto's.

I am just wondering why the "sleep" methods given by Robusto and CMS's link are not preferred. How would that be different than the settimeout method since they both introduce a pause in the code? (settimeout pauses before the function is executed, the sleep method pauses before the next line is executed.)

+1  A: 

As far as I know, setTimeout() is the only way to do it.

function write(out) {
  alert(out);
}

// ...

write('abc');
setTimeout(function() { write('xyz')}, 500);
Hooray Im Helping
Thanks, but settimeout does not work for what I am trying to do.
Jamex
+7  A: 

A sleep-Method is not available because JavaScript execution blocks the browser, so a sleep-Method would block the browser for 500msec, do you really want to have your browser not responding for half an second?

Use setTimeout as suggested.

Tobias P.
Thanks, but settimeout does not work for what I am trying to do.
Jamex
@Jamex yes it does - you just have to adjust what you want to do. Burning CPU time in a busy loop is a profoundly bad idea, especially when you're doing it to somebody else's computer.
Pointy
+4  A: 

You can use setTimeout so that it almost appears the code runs on two lines:

write('abc')
setTimeout(function() {
write('xyz')
},500)
Joel Coehoorn
Thanks, but settimeout does not work for what I am trying to do.
Jamex
A: 

The following is clunky and ugly, but it should work for what you want.

// time arg is in milliseconds
function delay(time) {
  var d1 = new Date();
  var d2 = new Date();
  while (d2.valueOf() < d1.valueOf() + time) {
    d2 = new Date();
  }
}
Robusto
Thanks Robusto.
Jamex
lol I was writing a custom sleep myself on jsfiddle, and it works really ugly :)
Anurag
This is a terrible, terrible, terrible idea. I would like to fill up the comment block, in fact, with the word "terrible", but somebody would probably mod out the comment. **Burning time on somebody else's CPU is rude**. At **least** have the function refuse to "delay" for more than 1/4 second or so (250ms). Still rude, but at least it's pretty brief.
Pointy
What Pointy said.
Tim Down
@Pointy: Hence my preface "ugly and clunky"; I just wanted to show that something like what the OP asked for *is* technically possible. I would never do this in my own code.
Robusto
+2  A: 

I don't know what you're trying to do here, but here's one concrete reason for why a custom sleep may not work for your purposes assuming the browser freezing up is a non-issue for you.

Are you manipulating the DOM by any chance between those two write commands? If you are, then it simply will not work (as perceived by an end user), although the DOM nodes will be constructed/updated in memory, the display will not get updated as that part is not synchronous. The processor is locked up in that loop, and both the DOM updates will refresh on screen when that loop finishes. See this example.

Ideally, you should see "Hello", and after 5 seconds, "World" on the screen. However, on Chrome, Safari, and Firefox, you would see both "Hello" and "World" at the end of 5 seconds. The console logs prove that the DOM node is constructed in memory, but is not refreshed on screen until the end as you can see yourself.

Anurag
Very good point!!
Pointy
@Anurag, I was trying to do a document.onclick and grab the id of whatever element that I clicked on. The problem was that after the settimeout, the click event was lost. Another user (jimr) was kind enough to write a short routine that preserves the event for me. I don't know JS syntax enough to understand good codes. I only write pseudo logic codes and try to search for the correct syntax, but it is difficult to google for answers if I don't know the key words/ideas. Thanks.
Jamex
@Jamex - I think if you update the question with the details from your above comment, you'll get a lot more helpful and better answers once users understand the problem better.
Anurag
@Jamex (and Anurag) – I think this original question is [already answered](http://stackoverflow.com/questions/3048005/document-onclick-settimeout-function-javascript-help/3048965#3048965) in the other question you referred to (though not perfectly).
Marcel Korpel
A: 

In JavaScript 1.7, using yield with async.js, you can do the following:

var yourFunction = _(function () {
    write("abc");
    yield to.sleep(.500);
    write("xyz");
});
Eli Grey
Thanks for the info. It seems that yield is still unknown to many.
Jamex