• 几个常用的函数


    1. export function debounce(fn, delay = 300) { //防抖默认300毫秒
    2. let timer;
    3. return function () {
    4. var args = arguments;
    5. if (timer) {
    6. clearTimeout(timer);
    7. }
    8. timer = setTimeout(() => {
    9. fn.apply(this, args); // this 指向vue
    10. }, delay);
    11. };
    12. }
    13. //节流throttle代码(时间戳+定时器):
    14. export function throttle(func, delay = 300) {//默认300毫秒
    15. let timer = null;
    16. let startTime = Date.now();
    17. return function () {
    18. let curTime = Date.now();
    19. let remaining = delay - (curTime - startTime);
    20. let context = this;
    21. let args = arguments;
    22. clearTimeout(timer);
    23. if (remaining <= 0) {
    24. func.apply(context, args);
    25. startTime = Date.now();
    26. } else {
    27. timer = setTimeout(func, remaining);
    28. }
    29. }
    30. }
    31. // 去除 富文本其他的标签
    32. export function filterHtml(str) {
    33. if (str) {
    34. return str.replace(/<[^<>]+>/g, "").replace(/ /gi, "")
    35. }
    36. }
    37. // 时间转成时间戳
    38. export function timeTotimestamp(time) {
    39. if (time) {
    40. var date = new Date(time);
    41. var date = new Date(time.replace(/-/g, '/'));
    42. // var time1 = Date.parse(date)/1000; //会精确到秒
    43. var time1 = Date.parse(date); //会精确到毫秒
    44. return time1;
    45. }
    46. }
    47. // 时间戳转成日期
    48. export function timestampToDate(timestamp) {
    49. if (!timestamp || timestamp == null) {
    50. return '';
    51. } else {
    52. var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    53. var Y = date.getFullYear() + '/';
    54. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    55. var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    56. return Y + M + D;
    57. }
    58. }
    59. // 时间戳转成时间
    60. export function unixTimeToDateTime(unixtime) {
    61. if (!unixtime) {
    62. return ''
    63. }
    64. let y, m, d
    65. var now = new Date(unixtime * 1); // 依情况进行更改 * 1
    66. y = now.getFullYear();
    67. m = now.getMonth() + 1;
    68. d = now.getDate();
    69. return (
    70. y +
    71. "-" +
    72. (m < 10 ? "0" + m : m) +
    73. "-" +
    74. (d < 10 ? "0" + d : d) +
    75. " " +
    76. now.toTimeString().substr(0, 8)
    77. );
    78. }
    79. export function validEmail(email) {
    80. const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    81. return reg.test(email)
    82. }
    83. // 手机号验证
    84. export function isvalidPhone(str) {
    85. const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
    86. return reg.test(str)
    87. }

  • 相关阅读:
    teamtalk原理
    vim学习笔记01
    git提交代码冲突
    强缓存与协商缓存
    基于php经贸时间轴小程序毕业设计-附源码211617
    运筹学研究者关注的Github和CSDN账号
    FPGA project : fifo_sum
    DSPE-PEG-Hydrazide DSPE-PEG-HZ 磷脂-聚乙二醇-酰肼科研用化学试剂
    2022年亚太杯ABC题思路全解、代码、参考论文/2022年亚太地区数学建模思路全解
    YOLOv6 PyTorch模型转TensorRT
  • 原文地址:https://blog.csdn.net/weixin_49035434/article/details/127777700