<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定时任务+循环几次停止</title>
</head>
<body>
<div>
<span>内容</span><input type="text" id="content">
<span>次数</span><input type="text" id="count">
<span>间隔</span><input type="text" id="interval">
<button id="sure">确定</button>
</div>
<script>
let btn=document.getElementById('sure')
btn.onclick=function(){
var content = document.getElementById("content").value;
var count = document.getElementById("count").value;
var interval = document.getElementById("interval").value;
// 发送内容 次数 间隔
if(count<=0 || interval<=0 || content==''){
return
}else if(count==1){//发送一次不需要定时任务
console.log(content)
}else{// 发送一次以上
var i = 0;
// 先发送第一次的内容
console.log(content)
var timer = setInterval(()=> {
console.log(i++);
console.log(content)
// 再发送除了第一次后的所有次数的内容
if (i>count-2) {
clearInterval(timer);
}
},interval*1000)
}
}
</script>
</body>
</html>
09-01