index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. Page({
  2. global : {
  3. timer : null ,
  4. isRand : false
  5. },
  6. data: {
  7. dice : ['first','second','third','fourth','fifth','sixth'],
  8. buttonType : 'primary',
  9. buttonValue : '摇一摇',
  10. isShow:'hidden',
  11. num1 : 0,
  12. num2 : 0,
  13. num3 : 0,
  14. total: 0
  15. },
  16. shakeClick: function () {
  17. let me = this;
  18. this.global.isRand = !this.global.isRand;
  19. if ( this.global.isRand ) {
  20. this.global.timer = setInterval(function (){
  21. let num1 = Math.floor(Math.random()*6);
  22. let num2 = Math.floor(Math.random()*6);
  23. let num3 = Math.floor(Math.random()*6);
  24. me.setData({num1 : num1});
  25. me.setData({num2 : num2});
  26. me.setData({num3 : num3});
  27. me.setData({total : num1+num2+num3+3});
  28. },50);
  29. } else {
  30. clearInterval(this.global.timer);
  31. }
  32. this.setData({
  33. dice: this.data.dice,
  34. buttonType : (this.global.isRand) ? 'default' : 'primary',
  35. buttonValue : (this.global.isRand) ? '停止' : '摇一摇',
  36. isShow: (this.global.isRand) ? 'hidden':'show',
  37. });
  38. },
  39. })