grids_lottery.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // console.log(`获奖位置:${this.winingIndex}`)
  16. // 活动index值增加,即移动到下一个格子
  17. this.startIndex++;
  18. //startIndex是最后一个时一圈走完,重新开始
  19. if (this.startIndex >= this.domData.length - 1) {
  20. this.startIndex = 0;
  21. this.count++;
  22. }
  23. // 当跑的圈数等于设置的圈数,且活动的index值是奖品的位置时停止
  24. if (this.count >= this.totalCount && this.startIndex === this.winingIndex) {
  25. if (typeof this.callback === 'function') {
  26. setTimeout(function() {
  27. that.callback(that.startIndex,that.count); //执行回调函数,抽奖完成的相关操作
  28. }, 400);
  29. }
  30. clearInterval(this.timer);
  31. }else { //重新开始一圈
  32. if (this.count >= this.totalCount - 1) {
  33. this.speed += 30;
  34. }
  35. this.timer = setTimeout(function() {
  36. that.callback(that.startIndex,that.count);
  37. that.rollFn();
  38. }, this.speed);
  39. }
  40. }
  41. }
  42. module.exports = LotteryDraw;