-1

Possible Duplicate:
Sleep in Javascript

I want to make a sleep function in JavaScript, I have to stop any code execution before the user performs an action (like the alert() behaviour)

What I need is exactly: what should I insert here to make it work?

window.alert = function(message){
    var dialog = $('<div>'+message+'</div>').dialog({
        title:'Alert',

        buttons:{
            'Ok': function(){
                $(this).dialog('close').data('open', false);
                $(this).remove();
            }
        }
    }).data('open', true);
    //What to insert here?
}
Community
  • 1
  • 1
ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
  • 1
    This questions has been answered here: http://stackoverflow.com/questions/758688/sleep-in-javascript –  Apr 20 '12 at 12:52
  • no, I am overriding the standard alert, so I cannot pass a function as parameter, I want to execute the code AFTER the alert, not something in parameter – ilyes kooli Apr 20 '12 at 12:54
  • If you pretend my question is answered there, then what should I insert in my code to make it work??? – ilyes kooli Apr 20 '12 at 12:56
  • @kooliilies you can't just sleep. You're whole code will need to be changed. – noob Apr 20 '12 at 12:57
  • 1
    You just can't, you need to user either callbacks, either with the deferreds of Daniel A. White or with my solution. – Tronix117 Apr 20 '12 at 13:00
  • @kooliilies, I apologize, I was referring emulating a sleep function in JS, which that post *does* answer, but I think Daniel A. White, and Tronix117 have given you a couple of solutions. –  Apr 20 '12 at 13:05

3 Answers3

1

jQuery deferreds are the best way to do this. It provides a nice callback framework so that its easier to maintain callbacks.

I have made a new function because some browsers may not let you redefine alert.

myAlert = function(message){
var dfd = new $.Deferred();

var dialog = $('<div>'+message+'</div>').dialog({
    title:'Alert',

    buttons:{
        'Ok': function(){
            $(this).dialog('close').data('open', false);
            $(this).remove();
            dfd.resolve();
        }
    }
}).data('open', true);
return dfd.promise();
}

Then

myAlert(message).done(function() { callback here });

if you want what you commented on, it would be this way.

myAlert(1).done(function() { myAlert(2) });
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Wouldn't a `setTimeout()` implementation be better? BTW - I didn't downvote you! – Greg Apr 20 '12 at 12:56
  • 1
    no, because that works on a predetermined amount of time. Deferreds work with async operations. – Daniel A. White Apr 20 '12 at 12:58
  • I cannot use Jquery deferreds, how to achieve this? alert(1); alert(2); alert(2) should execute after alert(1) is dismissed – ilyes kooli Apr 20 '12 at 12:59
  • 1
    why can't you? what version of jquery are you using? – Daniel A. White Apr 20 '12 at 13:00
  • I have to use **alert** because the code is full of alerts, and othe programmers are willing to add other alerts too... – ilyes kooli Apr 20 '12 at 13:03
  • 1
    thats fine. but i change it to `myAlert` because some web browsers may not allow you to redefine `alert`. – Daniel A. White Apr 20 '12 at 13:03
  • **alert** I mean take **one** parameter, not **tow** in your example, you have to know which code to be executed after the alert, but I don't know.. – ilyes kooli Apr 20 '12 at 13:07
  • @kooliilies because JavaScript is interactive you can't stop the code execution. So there's no sleep function. The given answers show everything what's possible. – noob Apr 20 '12 at 13:11
1

You should use a callback here, because if you deactivate the javascript, the user will not be able to click on 'OK'

window.alert = function(message, callback){
    var dialog = $('<div>'+message+'</div>').dialog({
        title:'Alert',

        buttons:{
            'Ok': function(){
                $(this).dialog('close').data('open', false);
                $(this).remove();
                typeof callback=='function' && callback(); //check if callback, and execute it
            }
        }
    }).data('open', true);
}
alert('test',function(){console.log('The user has clicked on the ok');});

EDIT:

If you only want to pause alert execution and not the other code execution you can do something like this (I haven't tried it, but the idea is here):

var alertStack=[];
window.alert = function(message){
  if(!message || !alertStack.length)
    $('<div>'+(alertStack[0] || message)+'</div>').dialog({
      title:'Alert',

      buttons:{
        'Ok': function(){
          alertStack.shift()
          $(this).dialog('close').data('open', false);
          $(this).remove();
          alertStack.length && alert();
        }
      }
    }).data('open', true);

  message && alertStack.push(message);
}
alert('test');
alert('test2');
alert('test3');
Tronix117
  • 1,985
  • 14
  • 19
  • I know how to use callbacks!! I wrote several jQuery plugins... what I want is to use the standard alert (the code uses thousands of alert calls, would take years to fix it...) – ilyes kooli Apr 20 '12 at 13:01
  • @kooliilies you can stop JavaScript with `while(true){}` but this will kill the browser and will make it impossible to click on one of the buttons. You should use something like this there's no other solution!!! – noob Apr 20 '12 at 13:05
  • 1
    I uploaded the script to only pause successions of alert, it means `alert(1);alert(2);` will work, but not `alert(1);a=2;alert(2);`, if the first dialog is printed, `a` will already be `2` – Tronix117 Apr 20 '12 at 13:14
  • Thank you for your effort, actually what I want is that my new alert behaves **EXACTLY** like the original window alert – ilyes kooli Apr 20 '12 at 14:23
0

Use setTimeout() when you want to defer the execution of code but do not want it to be repeatedly executed.

Ram
  • 143,282
  • 16
  • 168
  • 197