timeQueue.js 885 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. exports.default = class {
  2. constructor(name) {
  3. this.queue = {};
  4. this.timer = -1;
  5. }
  6. action(){
  7. if ("{}" !== JSON.stringify(this.queue)) {
  8. for (let i in this.queue) this.queue[i][0]();
  9. this.timer = -1, this.begin();
  10. } else {
  11. this.stop();
  12. }
  13. }
  14. add(e){
  15. var t = "" + new Date().getTime() + Math.ceil(1000 * Math.random());
  16. return this.queue["" + t] = [e], -1 === this.timer && this.start(), t;
  17. }
  18. remove(e) {
  19. delete this.queue["" + e], "{}" === JSON.stringify(this.queue) && (this.timer = -1);
  20. }
  21. del() {
  22. this.queue = {}, "{}" === JSON.stringify(this.queue) && (this.timer = -1);
  23. }
  24. stop() {
  25. clearTimeout(this.timer), this.timer = -1;
  26. }
  27. start() {
  28. this.timer > -1 || this.action();
  29. }
  30. begin() {
  31. var that = this;
  32. this.timer = setTimeout(function () {
  33. that.action();
  34. }, 1000);
  35. }
  36. }