2013-08-31 40 views
1

我想建立一個進度條,在0.1秒的延遲之後完成,隱藏帶有剪輯效果的欄,然後延遲0.2秒後再次加載google.com 。window.location令人煩惱的JQuery代碼

我的代碼:

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>jQuery UI Progressbar - Custom Label</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 
    <style> 
    .ui-progressbar { 
    position: relative; 
    } 
    .progress-label { 
    position: absolute; 
    left: 50%; 
    top: 4px; 
    font-weight: bold; 
    text-shadow: 1px 1px 0 #fff; 
    } 
    </style> 
    <script> 
    $(function() { 
    var progressbar = $("#progressbar"), 
     progressLabel = $(".progress-label"); 

    progressbar.progressbar({ 
     value: false, 
     change: function() { 
     progressLabel.text(progressbar.progressbar("value") + "%"); 
     }, 
     complete: function() { 
     progressLabel.text("Complete!"); 
     $("#progressbar").delay(100).hide("clip").delay(200); 
     window.location = "http://www.google.com"; 
     } 
    }); 

    function progress() { 
     var val = progressbar.progressbar("value") || 0; 

     progressbar.progressbar("value", val + 3); 

     if (val < 99) { 
     setTimeout(progress, 100); 
     } 

    } 

    setTimeout(progress, 3000); 

    }); 
    </script> 
</head> 
<body> 

<div id="progressbar"><div class="progress-label">Loading...</div></div> 


</body> 
</html> 

的問題是,即使之前的影響都可能發生,頁面加載http://www.google.com。是否有某種我可以使用的時間延遲?

謝謝。

回答

1

setTimeout

complete: function() { 
     progressLabel.text("Complete!"); 
     $("#progressbar").delay(100).hide("clip").delay(200); 
     setTimeout(function() { 

     window.location.href = "http://www.google.com" 

     }, 2000); 
     } 

所以嘗試,它等待2秒。

Learn more about SetTimeOut

+0

Thanks.But,我應該把這段代碼放在$(「#progressbar」).delay(100).hide(「clip」)。delay(200)之下嗎? –

+0

感謝您的幫助。 –

+0

@GaurangTandon當您設置超時時,在參數 –

1

JavaScript是高併發語言......所有的東西基本上發生同時放...

弄好方式happend因此是包裝隨之而來的代碼放到匿名函數..

(function(){ 
    //code executed first here 
})(); 

(function(){ 
    //code executed second here 
})(); 

或者你可以自定義包裝方法並調用它們,當所有事情都結束時執行位置更改爲回調。

+0

不錯,獲得新知識。謝謝。但是這對我來說有點非常棘手。 –