setTimeout和setInterval的使用

322 阅读1分钟

setTimeout 允许我们在时间间隔后运行一次函数。 setInterval 允许我们重复运行一个函数,在时间间隔后开始,然后在该时间间隔内连续重复。 setTimeout(()=>alert('hello'),1000)//1秒后在页面出现警示句alert;

function sayHello(){
   console.log('hello!');
}
function stopSayingHello(){
   clearInterval(intervalID)
}
var intervalID = setInterval(sayHello, 1000);

setTimeout(stopSayingHello,  6000)

String.prototype.substr() str.substr(start[, length]) substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符。 start 是一个字符的索引。首字符的索引为 0,最后一个字符的索引为 字符串的长度减去1。