window.onload = function () { var data = ['Phone5', 'Ipad', '三星笔记本', '佳能相机', '惠普打印机', '谢谢参与', '50元充值卡', '1000元超市购物券'], txt = document.getElementById('txt'), //抽奖结果 play = document.getElementById('play'), //开始按钮 stop = document.getElementById('stop'), //结束按钮 timer = null; //计时器 flag = false; //定时器状态标记 play.style.backgroundColor = '#036'; //设置按钮初始颜色 stop.style.backgroundColor = 'gray'; play.onclick = playFun; //点击开始按钮事件 stop.onclick = stopFun; //按任意键事件 document.onkeyup = function () { //判断是否为空格键 if (event.keyCode == 32) { //判断定时器状态 if (flag == false) { playFun(); } else { stopFun(); } } } //开始抽奖 function playFun() { clearInterval(timer); //清除上一个定时器,避免重复添加 //设置定时器 timer = setInterval(function () { txt.innerHTML = data[Math.floor(Math.random() * data.length)]; //随机奖品 }, 100); play.style.backgroundColor = 'gray'; //修改按钮颜色 stop.style.backgroundColor = '#036'; flag = true; //定时器标记为开 } //结束抽奖 function stopFun() { clearInterval(timer); play.style.backgroundColor = '#036'; stop.style.backgroundColor = 'gray'; flag = false; //定时器标记为关 } }
键盘事件 建码
键盘信息
keyCode
打印信息
console.log()
clearInterval(定时器名称);清除定时器,以防加多层定时器
在哪调的函数this指谁
keyCoden属性用于得到键盘对应键的键码值
console.log(x)打印x,在调试的时候可以看见
var random = Math.floor(Math.random()*data.length);
EVENT对象的keyCoden属性用于得到键盘对应键的键码值
event对象的keyCoden属性用于得到键盘对应的键码值
console.log(event.keyCode);//打印 event对象的keyCoden属性
回车 13 空格 32 向下 40 向上 38 向左 37 向右 39 shift 16 ctrl 17 alt 18
setInterval在开定时器前,要先清定时器clearInterval(timer);--->使用timer要先定义timer=null;
此处判断是否正在抽奖,应该使用timer全局变量的属性来判断,
if(timer==null){ timer = setInterval(); }else{ clearInterval(timer); }
hh
//创建奖池数组,与定时器。 var data=['phone5','ipad','samsung','canon','hp','thankyou','shopcard','¥1000'], timer=null, flag=0; window.onload=function(){ var play=document.getElementById('play'), stop=document.getElementById('stop'); //鼠标抽奖开始 play.onclick=playFun; stop.onclick=stopFun; //键盘抽奖事件 document.onkeyup=function(e){ e=event || window.event; //event.keyCode获取事件键码 if(e.keyCode===13){ //判断当前flag的值 if(flag===0){ playFun(); }else { stopFun(); } } } } function playFun(){ var play=document.getElementById('play'), title=document.getElementById('title'); //先停止定时器,防止多次点击叠加定时 clearInterval(timer); //设定定时器执行函数与间隔 timer=setInterval(function(){ //Math.random取0~1中间随机浮点数(不包括0与1),再乘以奖池数组长度,并用Math.floor取整得到0到数组长度之间的随机整数。 var random=Math.floor(Math.random()*data.length); //通过随机数取到的数组项替换元素内容 title.innerHTML=data[random]; },50); //谁调用函数,this指谁。但此处不能用this替代play,否则键盘事件中this将指代document.onkeyup,document无法改变背景 play.style.background='#ccc'; //flag放到这里对单纯的onclick事件没有影响,但对键盘事件与键鼠交叉操作的事件会有影响 flag=1; } function stopFun(){ clearInterval(timer); var play=document.getElementById('play'); play.style.background='#036'; flag=0; }
console.log
主要是方便你调式javascript用的,你可以看到你在页面中输出的内容。
相比alert他的优点是:
他能看到结构化的东西,如果是alert,淡出一个对象就是[object object],但是console能看到对象的内容。
console不会打断你页面的操作,如果用alert弹出来内容,那么页面就死了,但是console输出内容后你页面还可以正常操作。
console里面的内容非常丰富,你可以在控制台输入:console,然后就可看到它有网页的各种提示。
键盘事件
onkeydown:按下键盘上任意键时触发,(按住不放会重复触发)
onkeypress:按下键盘上的字符键时触发
onkeyup:释放键盘上的键时触发,(即按住不会重复触发)
keyCode:event对象的keyCode属性用于得到键盘对应键的键码值,回车键为13。
定时器:
注意:使用timer前一定要进行初始化=====>var timer[];
timer=setInterval(function(){},50):每隔50ms执行一次函数
clearInterval(定时器名):清除定时器,再加定时器前需清除原来的定时器,防止多个定时器叠加
随机数:
Math.random():生成0-1的随机数
Math.floor():向下取整
var arr = ['三星手机','小米1','小米2','苹果','魅族','50元话费','谢谢光临','血压仪','100元代金券','慕课网职业路径'];
var timer;
var flag = 0;
window.onload = function(){
var play = document.getElementById('play');
var stop = document.getElementById('stop');
//鼠标抽奖
play.onclick = playFun;
stop.onclick = stopFun;
//键盘抽奖
document.onkeyup = function(event){
event = event || window.event;
if(event.keyCode == 13){
if(flag == 0){
playFun();
}else if(flag == 1){
stopFun();
}
}
}
}
function playFun() {
var title = document.getElementById('title');
var play = document.getElementById('play');
if(flag==0){
timer = setInterval(function(){
var random = Math.floor(Math.random()*arr.length);
title.innerHTML = arr[random];
},100)
play.style.background="#333"
flag = 1;
}
}
function stopFun(){
clearInterval(timer);
flag = 0;
play.style.background="#113"
}