码迷,mamicode.com
首页 > 编程语言 > 详细

javascript BOM中的定时器

时间:2019-08-31 17:28:27      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:new   时间   距离   enter   alt   put   内容   star   parse   

定时器

setInterval   类似循环

语法:

var i = 0;

var  timer  =  setInterval (function(){

console.log(i);

i++;

if(i==10){

//清楚定时器

clearInterval(timer)

}

},1000);  //这里的1000指的是毫秒

setTimeOut   类似定时闹钟  在多少毫秒以后,执行一次    

小练习:2020倒计时

代码如下:

 

<style type="text/css">

p{

font-size: 20px;

color: #666;

}

</style>

<body>

<h1>距离2020年还有:</h1>

<p id="time"></p>

<script>

var oPo = document.getElementById(‘time‘);

function task(){

//此时此刻的时间

var now = new Date();

//2020年的时间

var target = new Date("2020/01/01 00:00:00");

var s = parseInt((target-now)/1000); //秒

if(s == 0){

//清除定时器

clearInterval(timer);

oPo.innerHTML = "新年快乐!";

}else{

var days = parseInt(s/3600/24);

var hs = parseInt(s/3600%24);

var mins = parseInt(s/60%60);

var ss =s%60;

oPo.innerHTML = days+"天"+hs+"小时"+mins+"分"+ss+"秒";

}

}

var timer = setInterval(task,1000);

</script>

</body>

效果图:

技术图片

 

练习题:随机点名

代码如下:

<style>

form{

width: 400px;

margin: 0px auto;

text-align: center;

background-color: #FAEBD7;

}

#start{

width: 100px;

height: 50px;

background-color: green;

font-size: 18px;

border: none;

outline: none;

border-radius: 8px;

}

#end{

width: 100px;

height: 50px;

background-color: red;

font-size: 18px;

border: none;

outline: none;

border-radius: 8px;

}

#Sname{

width: 200px;

height: 100px;

font-size: 20px;

font-weight: bold;

line-height: 100px;

float: left;

background-color: #FAEBD7;

color: green;

}

#Stime{

width: 200px;

height: 100px;

font-size: 24px;

font-weight: bold;

line-height: 100px;

float: left;

background-color: #FAEBD7;

color:green;

}

.hint{

width: 400px;

height: 100px;

font-size: 20px;

font-weight: bold;

line-height: 100px;

float: left;

background-color: #FFB6C1;

}

.h_none{

display: none;

}

</style>

<body>

<form action="">

<input type="button" value="开始点名" id="start">

<input type="button" value="停止点名" id="end">

<div id="Sname"></div>

<div id="Stime"></div>

<div id="hint" class="h_none"></div>

</form>

<script>

var stds = ["陈官耀","丰林","郭雷康","苟丽特","龚子威","黄俊雄","胡涛","贺宇航","侯政强","姜涵","姜驱寒","卢成","李宪","李星骆","彭正威","田文强","王飞","徐名扬","徐淑娟","袁成新","袁冠捷","杨乾越","叶文杰","尹逍宇","俞振雄","周朝兵","张海峰","张利","周雨芹","熊威"];

//定义一个变量

var timer;

var timer1;

var S_name;

var tim;

//开始点名时间

document.getElementById("start").onclick = function () {

timer = setInterval(students,50);

timer1 = setInterval(times,50);

}

//结束点名事件

document.getElementById("end").onclick = stop;

//定义开始的函数

function students(){

var num = Math.floor(Math.random()*stds.length);

S_name = stds[num];

document.getElementById(‘Sname‘).innerHTML = S_name;

document.getElementById("hint").className = "h_none";

}
//定义次数的函数
function times(){ 
tim = Math.ceil(Math.random()*10);
document.getElementById("Stime").innerHTML = tim;
}

//定义结束的次数

function stop(){

clearInterval(timer);

clearInterval(timer1);

document.getElementById("hint").className = "hint"

var S_hint = "恭喜"+S_name+"同学中奖了!"+"抄"+tim+"遍,加油袄。"

document.getElementById("hint").innerHTML = S_hint;

}

</script>

</body>

效果图:

技术图片

技术图片

技术图片

点击开始点名,随机出现30个名字中随机循环名字和次数,当点击停止点名的时候,随机点名会停止,次数也会停止,显现出来隐藏的内容。

 

 

 

 

 

javascript BOM中的定时器

标签:new   时间   距离   enter   alt   put   内容   star   parse   

原文地址:https://www.cnblogs.com/hyh888/p/11436180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!