index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. export function arrToIds(arr){
  2. var new_ids = '';
  3. for(var i = 0; i < arr.length; i++){
  4. new_ids += ','+arr[i].id;
  5. }
  6. if(new_ids!=''){
  7. new_ids = new_ids.substr(1);
  8. }
  9. return new_ids;
  10. }
  11. // 时间格式化
  12. export function timeDate(timestamp) {
  13. var date = new Date(timestamp)
  14. var Y = date.getFullYear() + '-'
  15. var M = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) + '-' : (date.getMonth() + 1) + '-'
  16. var D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate()
  17. return Y + M + D
  18. }
  19. /**
  20. * 函数防抖 (只执行最后一次点击)
  21. */
  22. export const Debounce = (fn, t) => {
  23. let delay = t || 500;
  24. let timer;
  25. console.log(fn)
  26. console.log(typeof fn)
  27. return function () {
  28. let args = arguments;
  29. if(timer){
  30. clearTimeout(timer);
  31. }
  32. timer = setTimeout(() => {
  33. timer = null;
  34. fn.apply(this, args);
  35. }, delay);
  36. }
  37. };
  38. /*
  39. * 函数节流
  40. */
  41. export const Throttle = (fn, t) => {
  42. let last;
  43. let timer;
  44. let interval = t || 500;
  45. return function () {
  46. let args = arguments;
  47. let now = + new Date();
  48. if (last && now - last < interval) {
  49. clearTimeout(timer);
  50. timer = setTimeout(() => {
  51. last = now;
  52. fn.apply(this, args);
  53. }, interval);
  54. } else {
  55. last = now;
  56. fn.apply(this, args);
  57. }
  58. }
  59. };