web前端必须功法之一:暂停取值
案例效果:
<style>
#box{
border: 1px solid #cccccc;
width: 300px;
height: 300px;
text-align: center;
line-height: 300px;
font-size: 50px;
}
</style>
<div align = "center">
<div id="box">
今天吃什么?
</div>
<br>
<input type="button" value="开始" id="start" />
<input type="button" value="停止" id="stop" />
</div>
<script>
var arr = ["火锅","烧烤","快餐便当","特色小食","意面披萨","韩式料理","轻食沙拉","能量西餐","中国菜"];
var box = document.getElementById("box");
var start = document.getElementById("start");
var stop = document.getElementById("stop");
var index = 0;
var timerId;
stop.style.display = "none";
start.onclick = function(){
timerId = setInterval(function(){
box.innerHTML = arr[index];
index++;
if(index > arr.length - 1){
index = 0;
}
},50)
stop.style.display = "block";
start.style.display = "none";
}
stop.onclick= function(){
clearInterval(timerId);
start.style.display = "block";
stop.style.display = "none";
}
</script>
暂停取值总结
1.要给开始按钮绑定点击事件
2.获取当前数组中的值并赋值给我们的div,并更新索引
3.要判断索引是否超过
|