From e9b131b8e35e8549f7352cd1dad061109f048730 Mon Sep 17 00:00:00 2001 From: Henry <617822642@qq.com> Date: Mon, 8 Oct 2018 21:48:19 +0800 Subject: [PATCH] fix time problem setTimeout would go wrong in some browser when the second argument is less than zero --- snippets/throttle.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/throttle.md b/snippets/throttle.md index e919bab24..77bfacc52 100644 --- a/snippets/throttle.md +++ b/snippets/throttle.md @@ -19,12 +19,14 @@ const throttle = (fn, wait) => { inThrottle = true; } else { clearTimeout(lastFn); + let time = 0; lastFn = setTimeout(function() { if (Date.now() - lastTime >= wait) { fn.apply(context, args); lastTime = Date.now(); } - }, wait - (Date.now() - lastTime)); + time = wait - (Date.now() - lastTime) + }, time>0 ? time : 0); } }; };