js 节流和防抖(记录)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>节流防抖</title>
</head>
<body>
	<input type="text" id="input">
</body>
<script>
    /*节流*/
    function throttle(fn,times) {
       var timer = null;
       return function () {
           if(timer) return;
           timer = setTimeout( ()=> {
               fn();
               timer = null;
           },times)
       }
    }


   /*防抖*/
   function debounce(fun,times) {
       var timer = null;
       return function () {
           clearTimeout(timer);
           timer = setTimeout(function () {
               fun();
           },times)
       }
   }
    function fun1() {
        console.log('防抖')
    }
    function fun2() {
        console.log('节流')
    }
    var ob = document.getElementById('input');
	ob.oninput = throttle(fun2,1000)


</script>
</html>

你可能感兴趣的:(JavaScript,javascript)