antiThrottling.js 977 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export default {
  2. keyList: {},
  3. uniDoFunc(e = {}) {
  4. e.type = e.type || 'thro';
  5. e.instant = (!e.instant?true : e.instant);
  6. switch (e.type) {
  7. case 'anti':
  8. {
  9. if (e.instant && !this.keyList[e.key]) {
  10. e.success && e.success();
  11. }
  12. clearTimeout(this.keyList[e.key])
  13. this.keyList[e.key] = setTimeout(() => {
  14. e.success && e.success();
  15. this.releaseKey(e.key)
  16. }, e.time || 300);
  17. break;
  18. }
  19. case 'thro':
  20. {
  21. if (!this.keyList[e.key]) {
  22. if (e.instant) {
  23. this.lockKey(e.key);
  24. e.success && e.success();
  25. } else {
  26. this.lockKey(e.key);
  27. }
  28. if (e.time) {
  29. setTimeout(() => {
  30. if (!e.instant) {
  31. e.success && e.success();
  32. }
  33. this.releaseKey(e.key)
  34. }, e.time)
  35. }
  36. } else {
  37. e.fail && e.fail()
  38. }
  39. break;
  40. }
  41. }
  42. },
  43. releaseKey(key) {
  44. delete this.keyList[key];
  45. },
  46. lockKey(key) {
  47. this.keyList[key] = true;
  48. }
  49. }