在JavaScript中睡觉?

在JavaScript中睡觉?,javascript,jquery,Javascript,Jquery,可能重复: 我想在JavaScript中创建一个sleep函数,我必须在用户执行操作(如alert()行为)之前停止任何代码执行 我需要的正是:我应该在这里插入什么使它工作 window.alert = function(message){ var dialog = $('<div>'+message+'</div>').dialog({ title:'Alert', buttons:{ 'Ok': fu

可能重复:

我想在JavaScript中创建一个sleep函数,我必须在用户执行操作(如alert()行为)之前停止任何代码执行

我需要的正是:我应该在这里插入什么使它工作

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?
}
window.alert=功能(消息){
变量对话框=$(''+消息+'')。对话框({
标题:“警报”,
按钮:{
“确定”:函数(){
$(此).dialog('close').data('open',false);
$(this.remove();
}
}
}).数据(“打开”,真实);
//在这里插入什么?
}
是最好的方法。它提供了一个很好的回调框架,因此更容易维护回调

我创建了一个新函数,因为某些浏览器可能不允许您重新定义
警报

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();
}
如果你想得到你的评论,应该是这样的

myAlert(1).done(function() { myAlert(2) });

您应该在这里使用回调,因为如果您停用javascript,用户将无法单击“确定”

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');});

当您想延迟代码执行但不想重复执行时,请使用setTimeout()。

这里已经回答了这个问题:不,我正在覆盖标准警报,因此我不能将函数作为参数传递,我想在警报后执行代码,而不是参数中的某个内容如果您假装我的问题在那里得到了回答,那么我应该在我的代码中插入什么来让它工作呢?@Kooliies你不能只是睡觉。您的整个代码都需要更改。您就是不能,您需要使用回调,或者使用Daniel A.White的延迟,或者使用我的解决方案。使用
setTimeout()
实现不是更好吗?顺便说一句,我没有投你反对票!不,因为它在预定的时间内工作。延迟与异步操作一起工作。我不能使用Jquery延迟,如何实现这一点?警报(1);警报(2);警报(2)应在警报(1)解除后执行为什么不能?您使用的是什么版本的jquery?没关系。但我将其更改为
myAlert
,因为某些web浏览器可能不允许您重新定义
alert
。我知道如何使用回调!!我写了几个jQuery插件。。。我想要的是使用标准警报(代码使用数千个警报调用,需要几年才能修复…@koolilies您可以使用
while(true){}
停止JavaScript,但这将杀死浏览器,并使其无法单击其中一个按钮。你应该使用这样的东西,没有其他的解决方案!!!我上传的脚本只暂停连续的警报,这意味着
警报(1);警报(2)将起作用,但不
警报(1);a=2;警报(2)
,如果打印第一个对话框,
a
将已经是
2
谢谢您的努力,实际上我想要的是我的新警报的行为与原始窗口警报完全相同
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');});
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');