Skip to content

Instantly share code, notes, and snippets.

@nevergiveup-j
Last active August 29, 2015 14:04
Show Gist options
  • Save nevergiveup-j/618e16a35349a69f1c6c to your computer and use it in GitHub Desktop.
Save nevergiveup-j/618e16a35349a69f1c6c to your computer and use it in GitHub Desktop.
jQuery 数组随机抽奖
/**
* @Description: jQuery 数组随机抽奖
* @Author: wangjun
* @Update: 2014-07-22 17:00
* @Github URL: https://github.com/nevergiveup-j
*/
!function($){
"use strict";
var lottery = {
init: function() {
var that = this;
that.$number = $('#J_lottery_number');
if ( !that.$number.length ) {
return;
};
that.$btnStart = $('#J_lottery_btn_start');
that.$btnEnd = $('#J_lottery_btn_end');
that.arrayData = iphoneData;
that.arrayLen = that.arrayData.length;
that.T_START = null;
if ( !that.arrayLen ) {
return;
}
that.bind();
},
bind: function() {
var that = this;
// 开始
that.$btnStart.bind('click', function() {
that.startCycle();
that.$btnStart.addClass('btn-startLottery-ban');
that.$btnEnd.removeClass('btn-endLottery-ban');
});
// 结束
that.$btnEnd.bind('click', function() {
that.$btnStart.removeClass('btn-startLottery-ban');
that.$btnEnd.addClass('btn-endLottery-ban');
clearInterval( that.T_START );
})
},
/**
* 开始循环
*/
startCycle: function() {
var that = this;
var number = 0,
iphone = '';
that.T_START = setInterval(function() {
number = that.getRandom(0, that.arrayLen);
iphone = that.arrayData[number];
that.$number.html( iphone );
}, 100);
},
/**
* 获取随机数字
*/
getRandom: function(start, end) {
return Math.floor(Math.random() * (end - start) + start);
}
}
$(function(){
lottery.init();
})
}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment