1.setTimeout方法,可以實(shí)現(xiàn)類似多線程的效果
參數(shù):code(some JavaScript code),定時(shí)執(zhí)行時(shí)間,單位默認(rèn)為毫秒
返回值:定時(shí)器id值
說明:clearTimeout方法可以取消。? ? setTimeout。
setTimeout(alerthello,2000);
fuction alerthello(){
? ? alert(“hello”);
? }
//瀏覽器打開后2S后彈出“hello”,且只執(zhí)行一次
2.setInterval方法,可以實(shí)現(xiàn)間歇調(diào)用
參數(shù):jscode,time
返回值:定時(shí)器id
setInterval(alertworld,2000);
fuction alertworld(){
? alert(“world”);
}
//每2S彈出world,無限執(zhí)行。
說明:停止setInterval的方法是clearInterval
這兩個(gè)方法都屬于BOM-window對象