js节流与防抖

//防抖
	function debounce(fn, delay) {
		let timer = null;
		return function () {
			if (timer) {
				clearInterval(timer)
			}
			timer = setTimeout(fn, delay)
		}
	}
	//节流
	function throttle(fn, delay) {
		let flag = true;
		return function () {
			if (!flag) return;
			flag = false;
			setTimeout(() => {
				fn();
				flag = true;
			}, delay)
		}
	}

 

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