javascript
JavaScript学习05 定时器
?
JavaScript學(xué)習(xí)05 定時(shí)器
?
定時(shí)器1
用以指定在一段特定的時(shí)間后執(zhí)行某段程序。
setTimeout():
格式:[定時(shí)器對(duì)象名=] setTimeout(“<表達(dá)式>”,毫秒)
功能:執(zhí)行<表達(dá)式>一次。
例子:
<!DOCTYPE html> <html><head><title>timer1.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">function count(){setTimeout("alert('執(zhí)行成功!')",7000);}</script></head><body><input type="button" value="點(diǎn)擊我啊" onclick="count();"></body> </html>?
?
定時(shí)器2
以一定的時(shí)間為間隔,不斷地重復(fù)執(zhí)行表達(dá)式。
setInterval():
格式:[定時(shí)器對(duì)象名=] setInterval(“<表達(dá)式>”,毫秒)
功能:重復(fù)執(zhí)行<表達(dá)式>,直至窗口、框架被關(guān)閉或執(zhí)行clearInterval。
clearInterval():
格式:clearInterval(定時(shí)器對(duì)象名)
功能:終止定時(shí)器
例子:
<!DOCTYPE html> <html><head><title>timer2.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">var sec = 0;var timer = setInterval("count();",1000);//頁面加載的時(shí)候即開始計(jì)時(shí)function count(){document.getElementById("num").innerHTML = sec++;}function stopCount(){clearInterval(timer);//停止定時(shí)器的運(yùn)行 }</script></head><body><font color="red" id="num">0</font><input type="button" value="停止" onclick="stopCount();"></body> </html>?
參考資料
圣思園張龍老師Java Web視頻教程。
W3School JavaScript教程:http://www.w3school.com.cn/js/index.asp
英文版:http://www.w3schools.com/js/default.asp
?
JS Timing:http://www.w3school.com.cn/js/js_timing.asp
?
轉(zhuǎn)載于:https://www.cnblogs.com/mengdd/p/3680863.html
總結(jié)
以上是生活随笔為你收集整理的JavaScript学习05 定时器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 进程Process基本的操作说明
- 下一篇: Spring Boot 10:处理Jso