uza*_*y95 13 javascript multithreading settimeout
我想在页面上分开线程,以防止gui冻结.为此,我运行的函数将使用setTimeout在另一个线程内冻结gui但仍然冻结.
代码和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>
Run Code Online (Sandbox Code Playgroud)
即使你通过setTimeout它委派执行仍然会在同一个单独的线程中执行,它只会等待它在队列中的时间,并将推迟任何其他活动,直到它完成.
请参阅"JS忍者的秘密"一书中的精彩图片:
