2009-06-24 53 views
5

有谁知道在下一行被系统读取之前,我该如何在javascript中进行睡眠?如何在javascript中进行睡眠?

例如:

1 var chkResult = Validation(); 
    2 //sleep here for for 10 sec before the next line been read  
    3 
    4 document.getElementById('abc').innerHTML = chkResult; 

在这个例子中,我怎样才能使第2行javascript的睡眠/等待10秒继续阅读第4行过吗?我曾尝试setTimeout('',10000);但它不工作,似乎对我来说还是......

+0

睡在JavaScript是一个糟糕的主意,因为它会导致浏览器的用户界面变得无法响应 – cobbal 2009-06-24 06:04:56

+0

我担心会做十秒的验证。那时是服务器响应的时间吗?如果您使用ajax调用进行验证,那么您是否应该获得回调? – Nosredna 2009-06-24 06:12:08

回答

16

I Have the Hat已经给出了正确的提示。使用setTimeout method 10秒后,执行你的第四行代码:

var chkResult = Validation(); 
var timeout = window.setTimeout(function() { 
    document.getElementById('abc').innerHTML = chkResult; 
}, 10000); 

在一个变量存放超时ID,如果你想clear a timeout都能得心应手。

5

尝试

setTimeout(function() { return true; }, 10000); 

第一个参数预期的功能。这是来自记忆;我没有测试过它。

编辑:古姆博说什么......迟到......不知道我在想什么。

+0

您可以给一个函数或一串代码执行。见https://developer.mozilla.org/en/DOM/window.setTimeout – Gumbo 2009-06-24 06:06:53

2

setTimeout在JavaScript解释器中启动一个单独的执行“线程”。您需要传递一个函数并设计脚本,以使setTimeout运行的函数能够继续执行调用函数的位置 - 从而模拟睡眠。

1

在JavaScript将控制权交还浏览器之前,延迟不会发生,因此您的第4行将在setTimeout开始之前执行。

你应该根据事件做一切事情。

0

我建议这是一个通用的解决方案:

function sleep (ms, args, obj) { 
    /* Called at the top of a function to invoke a delay in processing that 
     function 

     Returns true if the function should be executed, and false if the sleep 
     timer is activated. 

     At the end of the sleep tiemout, the calling function is re-called by 
     calling it with "apply (obj, args || [])". 
    */ 

    var caller = sleep.caller; 
    if (caller.sleepTimer) { 
     /* if invoked from timeout function, delete timer and return false */ 
     delete caller.sleepTimer; 
     return true; 
    } 

    /* Create timer and re-call caller at expiry */ 
    caller.sleepTimer = window.setTimeout (function() { 
     caller.apply (obj || null, args || []); 
    },ms); 

    return false; 
    }  

使用的例子:

function delayed() { 
    if (!sleep (5000)) return; 

    document.body.innerHTML += "<p>delayed processing</p>"; 
} 

function delayed_args (a, b) { 
    if (!sleep (10000, arguments)) return; 

    document.body.innerHTML += "<p>args : (" + a + ", " + b + ")</p>"; 
} 

function delayed_method_args (a,b) { 
    if (!sleep (20000, arguments, this)) return; 

    document.body.innerHTML += "<p>method_args " + this + " (" + a + ", " + b + ")</p>"; 
} 

测试在Opera和Firefox 3.0

3
1 var chkResult = Validation(); 
    2 alert("wait ten seconds, then press ok"); 
    3  
    4 document.getElementById('abc').innerHTML = chkResult; 

的前述代码代表睡眠任务到并行处理器。这有点麻烦,因为你必须使用DSL来实现这种线程风格。

0

Javascript必须与浏览器共享线程,因此您想要的停顿类型看起来非常像浏览器被冻结了10秒,是否有可能。诸如“alert”和“prompt”之类的函数可以执行此操作。为此,它们是邪恶的,因为循环中的单个警报可能会导致整个浏览器瘫痪,唯一的出路是强制退出它。

解决这个问题的方法是基于事件的范例。 JavaScript具有头等功能,所以它们可以作为可以分配给某些事件的“回调”值传递。

setTimeout就是这样一个事件。其他人已经发布了代码,但总体而言,您只需将功能分配给某些事件,然后避开浏览器,这样就可以实现浏览业务。浏览器会让你知道什么时候发生了什么,并且你可以快速做出适当的回应,然后再次避开。通过这种方式,浏览器可以保持响应的外观。

如果您想了解更多

,看看这个问题: How is GUI and Game Program Flow compared to Web programs

0

但这样一来,你的行线将执行前行4