2009-06-24 56 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