common-utils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class CommonUtils {
  2. static selectWait(selector, func, times, interval) {
  3. var _times = times || 100, //100次
  4. _interval = interval || 500, //20毫秒每次
  5. _jquery = null,
  6. _iIntervalID;
  7. _iIntervalID = setInterval(() => {
  8. if (!_times) {
  9. clearInterval(_iIntervalID);
  10. }
  11. _times <= 0 || _times--;
  12. _jquery = $(selector);
  13. if (_jquery.length) {
  14. func && func.call(func);
  15. clearInterval(_iIntervalID);
  16. }
  17. }, _interval);
  18. return this;
  19. }
  20. static selectNotWait(selector, func, interval) {
  21. let _jquery,
  22. _interval = interval || 20,
  23. _iIntervalID;
  24. _iIntervalID = setInterval(() => {
  25. _jquery = $(selector);
  26. if (_jquery.length < 1) {
  27. func && func.call(func);
  28. clearInterval(_iIntervalID);
  29. }
  30. }, _interval);
  31. }
  32. static copyText(value, cb) {
  33. const textarea = document.createElement("textarea");
  34. textarea.readOnly = "readonly";
  35. textarea.style.position = "absolute";
  36. textarea.style.left = "-9999px";
  37. textarea.value = value;
  38. document.body.appendChild(textarea);
  39. textarea.select();
  40. textarea.setSelectionRange(0, textarea.value.length);
  41. document.execCommand("Copy");
  42. document.body.removeChild(textarea);
  43. if (cb && Object.prototype.toString.call(cb) === "[object Function]") {
  44. cb();
  45. }
  46. }
  47. /**
  48. * 休眠
  49. * @param {number} ms 休眠多少毫秒
  50. */
  51. static sleep(ms) {
  52. return new Promise((resolve, reject) => {
  53. setTimeout(() => {
  54. resolve("完成");
  55. }, ms);
  56. });
  57. }
  58. static checkType() {
  59. if (navigator && navigator.userAgent && /Mobi|Android|iPhone/i.test(navigator.userAgent)) {
  60. if (navigator && navigator.userAgent && /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  61. return 'ios';
  62. } else {
  63. return 'android';
  64. }
  65. } else {
  66. return 'pc';
  67. }
  68. };
  69. }