常见函数

防抖:

function debounce(fn, delay){
  let timer
  return function(...args) {
      if(timer){
        clearTimeout(timer)   
      }
      timer = setTimeout(() => {
        fn.apply(this,args)
     },delay)
    }
}    

节流:

function throttle(fn, delay){
  let last = 0
  return (...args) => {
    const = Date.now()
    if(now-last>delay){
     last = now
     fn.apply(this, args)   
   }       
  } 
}    

 冒泡排序:

function bubbleSort(arr) {
  let len = arr.length
  for(let i=0;i<len-1;i++){
    for(let j=0;j<len-1-i;j++) {
      if(arr[j]>arr[j+1]){
      let num = arr[j]
        arr[j] = arr[j+1]
        arr[j+1] = num      
    }    
   }
  }
  return arr
}

indexOf 去重:

function resetArr(arr){
  let res = []
  arr.foreach(item => {
    if(res.indexOf === -1){
      res.push(item)  
    }
  }) 
  return res
}    

  

posted @ 2022-02-25 15:27  落灯花  阅读(37)  评论(0编辑  收藏  举报