jQuery摇奖代码

一个jQuery写的摇奖小程序,中奖号码在0-999之间随机生成,使用setInterval()循环刷新随机数,当停止时调用clearTimeout()。
测试地址:http://lab.lemontv.me/award.html
代码:


var max = 999;

//随机函数
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
//切换随机数
function addNum() {
var i = rand(max);
if(i < 10) {
i = "00" + i;
}
else if(i < 100) {
i = "0" + i;
}
$("#number").html(i);
}

$(document).ready(function() {
//开始 随机数变换
$("#start").click(function() {
if($("#flag").val() == 0) {
t = setInterval("addNum()", 1);
$("#flag").val(1);
}
});
//停止 中奖号码确定
$("#stop").click(function() {
clearTimeout(t);
$("#flag").val(0);
});

});

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.