Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript函数防抖 #10

Open
gaowei1012 opened this issue Feb 19, 2019 · 0 comments
Open

javascript函数防抖 #10

gaowei1012 opened this issue Feb 19, 2019 · 0 comments

Comments

@gaowei1012
Copy link
Owner

防抖 (debounce): 将多次高频操作优化为只在最后一次执行,通常使用的场景是:用户输入,只需再输入完成后做一次输入校验即可。

// 将多高频次操作优化为最后一次执行, 优化性能
function debounc(fn, wait, immediate) {
	let timer = null;

	return function () {
		let args = arguments;
		let context = this;

		if (immediate && !timer) {
			fn.apply(context, args);
		}

		if (timer) clearTimeout(timer);
		timer = setTimeout(() => {
			fn.apply(context, args);
		}, wait);
	};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant