定时器
定时器分为两种
一种是一次性的,时间到就执行
var timer=setTimeout(function(){},触发的时间(单位毫秒));
清除的方法
clearTimeout(timer)
第二种是周期性的,根据设定的时间周期进行
var timer=setInterval(function(){},触发的时间(单位毫秒));
清除的方法
clearInterval(timer)
下面写一个简单的关于定时器的小案例——开心摇摇乐(随机点名系统)
开心摇摇乐
话不多说,上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>开心摇摇乐</title>
<style type="text/css">
.wrapper {
width: 99%;margin: 8px auto;
border: 1px solid #ddd;text-align: center;
background-color: pink;
}
.box li {
vertical-align: top;
display: inline-block;
width: 100px;height: 50px;
border: 2px solid #ddd;
border-radius: 15px;text-align: center;
line-height: 50px; margin: 5px;
}
.box li.change {
background-color: red;
color: #fff;font-weight: bolder;
}
.wrapper button {
display: inline-block;
}
.wrapper button {
border: none;width: 100px;
height: 50px;border-radius