定时器应用函数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定时器应用函数</title>
</head>
<body>
<script>
function invoke(f,start,interval,end){
if(!start) start = 0;//默认设置为0毫秒
if(arguments.length <= 2){//单次调用模式
setTimeout(f,start);//若干毫秒后的单词调用模式
}else{
setTimeout(repeat,start);//多次调用模式
function repeat(){//在上一行所示的setTimeout()中调用
var h = setInterval(f,interval);//循环调用f
//在end秒后停止调用,前提是end已经定义了
if(end) setTimeout(function(){clearInterval(h);},end);
}
}
}
function f(){console.log('ok')};
invoke(f,2000,'interval',4000);
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/studyh5/p/9322464.html