且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个简单的中奖抽奖小程序

更新时间:2022-10-03 14:14:56

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>
    <style type="text/css">
        #zhongjiang{
            width: 100px;
            height: 100px;
            background: red;
 
        }
 
    </style>
</head>
<body>
<div id="zhongjiang"></div>
 
<button id="J_start">开始</button>
<button id="J_stop">结束</button>
</body>
<script type="text/javascript">
    $(function(){
        var a = '';
        var user = [];
    var LIST   =  {
 
        init    :  function(){
            this.getData();
            this.bindEvent();
    },
 
    getData : function(){
        $.ajax({
     type: "POST",
     url: "choujiang.php",
     async:false,                 //这里一定要有设置,下面return user,否则uer没有值
    success:function(data){
        user = JSON.parse(data);
        }
            });
         return user;
        },
 
        startRotate :  function(){
             a = setInterval(function(){LIST.start()},100);
             $("#J_start").unbind();
        },
 
        stopRotate  :  function(){
            clearInterval(a);
            $("#J_start").bind('click',function(){LIST.startRotate()});
            alert($("#zhongjiang").text());
        },
 
        bindEvent   :  function(){
            $("#J_start").bind('click',function(){LIST.startRotate();});
            $("#J_stop").bind('click',function(){LIST.stopRotate();});
      },
 
       start  :   function() {
          var sum = user.length;
          var luckyuser = [];
          var rand = Math.floor(Math.random()*sum);
              $.each(user,function(i,n) {
                  if (rand == i) {
                      luckyuser = n;
                  }
              });
              $("#zhongjiang").empty().append(luckyuser.phone);
      
 
 
 
    }
 
 
     
 
    LIST.init();
         
})
 
 
 
 
 
</script>
 
</html>

采用bind方法,当点击开始后,去掉绑定的事件,点击暂停后,又将事件重新绑定,防止多次点击开始进行许多次的定时操作,这样就暂停不了了。

本文转自  陈小龙哈   51CTO博客,原文链接:http://blog.51cto.com/chenxiaolong/1734134