我想單獨在頁面上的線程來防止gui凍結。爲此,我正在運行一個函數,它會使用setTimeout在另一個線程中凍結gui,但仍然凍結。在javascript中的另一個線程內運行代碼
的代碼和jsbin鏈接低於:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"
type="text/javascript"></script>
<meta charset="utf-8" />
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<input type="button" value="düðme" id="btn" />
<script type="text/javascript">
$("#btn").on("click",function(){
$("#div1").html(new Date());
});
$(document).ready(function(){
setTimeout(function() { count(); },1);
});
function count(){
for(var i =0;i<100000;i++){
$("#div2").html(i);
}
$("#div2").append(new Date());
}
</script>
</body>
</html>
這仍然會在同一個線程中執行。一切都將在同一個線程中執行。唯一的例外是WebWorkers。 – mishik
但jquery ajax請求正在不同線程中工作,如果我們說'async:true'。我現在正在尋找關於網絡工作者的好信息。我需要在其他線程中運行某些東西,而gui仍然可以觸摸並從網絡加載另一個數據。 – uzay95
該ajax請求的回調仍將在同一個線程中執行。 'async:true'意味着瀏覽器在等待Ajax調用結果時可以花費一些時間處理其他任務。 – mishik