一个非常简单的循环代码,点击开始和停止,支持自定义次数
<html>
<head>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function() {
function test(){
$(".test").append("|->");
/*
$.ajax({
url:"/123.php",
type:"get",
timeout:"15000",
async:true,
success:function(data){
$(".test").append(data);
}
})
*/
}
$(".star").click(function(){
num = $('.num').val();
timer = setInterval(function() {
test();
if (num){
num--;
if (num <= 0) clearInterval(timer);
}
}, 1000);
})
$(".stop").click(function(){
clearInterval(timer);
})
})
</script>
</head>
<body>
<input type="text" class="num" size="5">
<button class="star">开始</button><button class="stop">停止</button>
<div class="test"></div>
</body>
</html>
https://cway.top