一切盡在代碼截圖中,愿意和大家一起成長!可以噴,畢竟是菜鳥! 好了,廢話不多說了,看具體實(shí)例代碼吧!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="wraper"></div> <div class="wraper1">abcd123441223432 </div> <button onclick="clearIt=window.clearInterval(clearIt)"> Stop</button> <script> window.onload = function () { $('.wraper').html('12343242342') setTimeout(function () { $('.wraper').hide(); setTimeout(function () { $('.wraper1')[0].style.backgroundColor = 'red'; console.log(new Date(),"in"); },20000); console.log(new Date(),"out"); },10000); } var clearIt=self.setInterval("setIntervalTest()",1000); function setIntervalTest () { console.log('++++'); } </script> <script type="text/javascript" src="jquery/jquery-min.js"></script> </body> </html> 


補(bǔ)充:下面看下setTimeout和setInterval的相同處及區(qū)別介紹
setTimeout和setInterval的相同處
setTimeout和setInteval 都是window對(duì)象的定時(shí)方法,它們語法基本上相同。它們都有兩個(gè)參數(shù),一個(gè)是將要執(zhí)行的代碼字符串,還有一個(gè)是以毫秒為單位的時(shí)間間隔,當(dāng)過了那個(gè)時(shí)間段之后就將執(zhí)行那段代碼。
setTimeout和setInterval的區(qū)別
setTimeout方法是定時(shí)程序,也就是在什么時(shí)間以后干什么。干完了就拉倒。
setInterval方法則是表示間隔一定時(shí)間反復(fù)執(zhí)行某操作。
如果用setTimeout實(shí)現(xiàn)setInerval的功能,就需要在執(zhí)行的程序中再定時(shí)調(diào)用自己才行。
如果要清除定時(shí)器則根據(jù)創(chuàng)建定時(shí)器時(shí)使用的方法調(diào)用不同的清除方法:
例如:
timer=setTimeout('startShow()',2000);clearTimeout(timer);或者:
timer=setInterval('startShow()',2000);clearInterval(timer);以上所述是小編給大家介紹的JS中SetTimeout和SetInterval使用初探,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注