grids_lottery.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function LotteryDraw(obj, callback) {
  2. this.timer = null; //计时器
  3. this.startIndex = obj.startIndex-1 || 0; //从第几个位置开始抽奖 [默认为零]
  4. this.count = 0; //计数,跑的圈数
  5. this.winingIndex = obj.winingIndex || 0;//获奖的位置
  6. this.totalCount = obj.totalCount || 6;//抽奖跑的圈数
  7. this.speed = obj.speed || 100;
  8. this.domData=obj.domData;
  9. this.rollFn();
  10. this.callback = callback;
  11. }
  12. LotteryDraw.prototype = {
  13. rollFn: function() {
  14. var that = this;
  15. // 活动index值增加,即移动到下一个格子
  16. this.startIndex++;
  17. //startIndex是最后一个时一圈走完,重新开始
  18. if (this.startIndex >= this.domData.length - 1) {
  19. this.startIndex = 0;
  20. this.count++;
  21. }
  22. // 当跑的圈数等于设置的圈数,且活动的index值是奖品的位置时停止
  23. if (this.count >= this.totalCount && this.startIndex === this.winingIndex) {
  24. if (typeof this.callback === 'function') {
  25. setTimeout(function() {
  26. that.callback(that.startIndex,that.count); //执行回调函数,抽奖完成的相关操作
  27. }, 400);
  28. }
  29. clearInterval(this.timer);
  30. }else { //重新开始一圈
  31. if (this.count >= this.totalCount - 1) {
  32. this.speed += 30;
  33. }
  34. this.timer = setTimeout(function() {
  35. that.callback(that.startIndex,that.count);
  36. that.rollFn();
  37. }, this.speed);
  38. }
  39. }
  40. }
  41. module.exports = LotteryDraw;