setTimeout為0的應用
在下面兩個輸入框里輸入字符,看看有什么不同?
A:沒有使用setTimeout
你輸入了:
B:setTimeout為0
你輸入了:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>setTimeout為0的應用</title>
<style type="text/css">
body { background:url(images/page-bg.gif); font-size:14px;}
</style>
</head>
<body>
<h1>setTimeout為0的應用</h1>
<p>在下面兩個輸入框里輸入字符,看看有什么不同?</p>
<p>
A:<input type="text" id="t1" name="t1"
onkeydown="document.getElementById('t1text').innerHTML='你輸入了:'+this.value;" value="" />沒有使用setTimeout
</P>
<div id="t1text">你輸入了:</div>
<p>
B:<input type="text" id="t2" name="t2"
onkeydown="var t2=this;setTimeout(function(){document.getElementById('t2text').innerHTML='你輸入了:'+t2.value},0);"
value="" />setTimeout為0</p>
<div id="t2text">你輸入了:</div>
</body>
</html>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>setTimeout為0的應用</title>
<style type="text/css">
body { background:url(images/page-bg.gif); font-size:14px;}
</style>
</head>
<body>
<h1>setTimeout為0的應用</h1>
<p>在下面兩個輸入框里輸入字符,看看有什么不同?</p>
<p>
A:<input type="text" id="t1" name="t1"
onkeydown="document.getElementById('t1text').innerHTML='你輸入了:'+this.value;" value="" />沒有使用setTimeout
</P>
<div id="t1text">你輸入了:</div>
<p>
B:<input type="text" id="t2" name="t2"
onkeydown="var t2=this;setTimeout(function(){document.getElementById('t2text').innerHTML='你輸入了:'+t2.value},0);"
value="" />setTimeout為0</p>
<div id="t2text">你輸入了:</div>
</body>
</html>
小結:
類似起線程。前者沒用settimeout ,所以是主線程去做的響應,所以在onkeydown 就觸發事件執行。得到的數據會有差異。