CountDown.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. onLoad () {
  6. this.TimeLabel = this.node.getComponent(cc.Label);
  7. //init
  8. this.Init();
  9. },
  10. Init()
  11. {
  12. this.curtTime = {'second':0,'millisecond':0,'secondBase':60,'millisecondBase':60};
  13. },
  14. InitTime(time)
  15. {
  16. this.TimeLabel.string = this.ShowTime(time);
  17. this.curtTime.second = time.second;
  18. this.curtTime.millisecond = time.millisecond;
  19. },
  20. CountDown(callback)
  21. {
  22. let times = this.curtTime.second*this.curtTime.millisecondBase + this.curtTime.millisecond;
  23. times--;
  24. this.schedule(function(){
  25. this.TimeMinus();
  26. this.TimeLabel.string = this.ShowTime(this.curtTime);
  27. if(this.curtTime.second*this.curtTime.millisecondBase + this.curtTime.millisecond == 0)
  28. {
  29. callback();
  30. }
  31. }.bind(this),0.01,times);
  32. },
  33. TimeMinus()
  34. {
  35. if((this.curtTime.millisecond-1) < 0)
  36. {
  37. this.curtTime.millisecond = this.curtTime.millisecondBase-1;
  38. this.curtTime.second--;
  39. return;
  40. }
  41. this.curtTime.millisecond--;
  42. },
  43. ShowTime(time)
  44. {
  45. let second = '';
  46. let millisecond = '';
  47. if(time.second<10)
  48. {
  49. second ='0'+time.second;
  50. }
  51. else
  52. {
  53. second = time.second;
  54. }
  55. if(time.millisecond<10)
  56. {
  57. millisecond = '0' + time.millisecond;
  58. }
  59. else
  60. {
  61. millisecond = time.millisecond;
  62. }
  63. return second+':'+millisecond;
  64. }
  65. });