淺析javascript的間隔調用和延時調用
用 setInterval方法可以以指定的間隔實現(xiàn)循環(huán)調用函數(shù),直到clearInterval方法取消循環(huán)
用clearInterval方法取消循環(huán)時,必須將setInterval方法的調用賦值給一個變量,然后clearInterval方法引用該變量。
<script type="text/javascript">
var n = 0;
function print(){
document.writeln(n);
if(n==1000){
window.clearInterval(s);
}
n++;
}
var s = window.setInterval(print, 10);
</script>
用setTimeout和clearTimeout完成延時調用,在指定的延遲時間之后運行指定函數(shù),只執(zhí)行一次。clearTimeout的用法同clearInterval方法的用法相同。
<script type="text/javascript">
function printTime(){
var time = new Date();
var year = time.getFullYear();
var month = (time.getMonth())+1;
var daynum = time.getDay();
var hour = time.getHours();
var min = time.getMinutes();
var sec = time.getSeconds();
var da = time.getDate();
var daystr;
switch(daynum){
case 0: daystr="星期日";
break;
case 1: daystr="星期一";
break;
case 2: daystr="星期二";
break;
case 3: daystr="星期三";
break;
case 4: daystr="星期四";
break;
case 5: daystr="星期五";
break;
case 6: daystr="星期六";
break;
default: daystr="";
}
var str = year+"年"+month+"月"+da+"日 "+daystr+" "+hour+": "+min+": "+sec;
document.getElementById("t").innerHTML = str;
window.setTimeout(printTime, 1000);
}
</script>
<body onload="printTime()">
<br/>
<div id="t"></div>
</body>
- 原生javascript圖片自動或手動切換示例附演示源碼
- 原生javascript實現(xiàn)圖片輪播效果代碼
- javascript實現(xiàn)iframe框架延時加載的方法
- javascript間隔定時器(延時定時器)學習 間隔調用和延時調用
- javascript延時加載之defer測試
- Javascript Function對象擴展之延時執(zhí)行函數(shù)
- javascript 不停(setInterval)/延時(setTimeout)函數(shù)使用實例
- JavaScript延時效果比較不錯的
- javascript延時重復執(zhí)行函數(shù) lLoopRun.js
- 原生javaScript實現(xiàn)圖片延時加載的方法
相關文章
javascript處理表單示例(javascript提交表單)
這篇文章主要介紹了javascript處理表單示例,處理 各種表單, 以及鏈接,按鈕的通用組件,需要的朋友可以參考下2014-04-04window.location.href的用法(動態(tài)輸出跳轉)
無論在靜態(tài)頁面還是動態(tài)輸出頁面中window.location.href都是不錯的用了跳轉的實現(xiàn)方案2014-08-08JavaScript中的call方法和apply方法使用對比
這篇文章主要介紹了JavaScript中的call方法和apply方法使用對比,需要的朋友可以參考下2015-08-08Javascript常用運算符(Operators)-javascript基礎教程
Javascript常用運算符(Operators)-javascript基礎教程...2007-12-12document.getElementById的簡寫方式(獲取id對象的簡略寫法)
在js編寫中,經常需要獲取id對象,如果直接用getElementById來獲取,代碼多而且老的瀏覽器不支持這屬性,所有大家可以考慮用下面的代碼。2010-09-09JavaScript 鍵盤event.keyCode值列表大全
event.keyCode值列表大全,對于需要根據(jù)鍵盤按鍵觸發(fā)相應事件的朋友需要。2010-08-08