JavaScript 抽奖

test.html

<html>
    <head>
        <title>我的摇奖程序title>
        <script src="myjs.js">script>
        <style>
            #name{
                color:red;
                font-size:36px;
            }
        style>
    head>
    <body>
        <br/>
        <br/>
        <div id="name">div>
        <br/>
        <br/>
        <input type="button" value="开始抽奖" onclick="start()">
        <input type="button" value="停止抽奖" onclick="stop()">
    body>
html>

myjs.js

var numbers=new Array();
numbers[0]="5元券";
numbers[1]="10元券";
numbers[2]="20元券";
numbers[3]="没抽中";

var timer;
var i=0;
function start(){
    //start timer
    i=100+Math.round(Math.random()*100);
    timer=setInterval(show,100);//5ms
}
function stop(){

    clearInterval(timer);
}
function show(){
    if(i==0){
        stop();
    }
    i--;

    var index=Math.round(Math.random()*(numbers.length-1));
    document.getElementById("name").innerHTML=numbers[index];
}

你可能感兴趣的:(javascript)