防抖一段时间内,最后一次触发

    1. function but(fn,i){
    2. let n;
    3. return function(){
    4. clearTimeout(n)
    5. n = setTimeout(() => {
    6. fn.call(this,i)
    7. },i);
    8. }
    9. }
    10. function a(b){
    11. console.log("防抖成功"+b)
    12. }
    13. var oinp = document.querySelector('input')
    14. oinp.addEventListener('click',but1(a,3000))

    节流一段时间内,最后一次触发

    1. function but1(fn,i){
    2. let n = true;
    3. return function(){
    4. if(!n) {return}
    5. n =false
    6. setTimeout(() => {
    7. fn.call(this,i)
    8. n = true
    9. }, i);
    10. }
    11. }
    12. function a(b){
    13. console.log("节流成功"+b)
    14. }
    15. var oinp = document.querySelector('input')
    16. oinp.addEventListener('click',but1(a,3000))