計時器setTimeout()
<!DOCTYPE?html>
<html>
<head>
<meta?charset="UTF-8">
<title>定時器</title>
<script?type?=?"text/Javascript">
//開始計時的函數
function?clock(){
var?time?=?new?Date();
var?attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
????document.getElementById("clock").value?=?attime;
}
//定義函數clock2(),實現點擊Start按鈕后延遲5秒打開百度在首頁
function?clock2(){
setTimeout(window.open("http://www.baidu.com","_blank","width=600px,height=400px"),5000);
}
//每隔100毫秒調用函數clock(),并將返回值賦給變量i
var?i?=?setInterval("clock()",1000);
</script>
</head>
<body>
<form>
<input?type="text"?id="clock"?size="4"??/>
?????<input?type="button"?value="Stop"?onclick="clearInterval(i)"/><br>
?????<input?type?=?"button"?value?=?"Start"?onclick?=?"clock2()"/>
??</form>
</body>
</html>函數clock2()要實現的功能是點擊Start按鈕后延遲5秒打開百度。執行完之后怎么不延遲直接打開了呢?
2017-01-16
個人理解:點擊這個動作是即時的,而setTimeout是延時,兩者并不能用在一起
2017-01-12
var?i?=?setInterval("clock()",1000); 換成var?i?=?setInterval("clock",1000);