forked from YvetteLau/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,46 +33,42 @@ | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script> | ||
<script> | ||
window.onload = function () { | ||
function throttle(func, wait, options = { | ||
leading: true, | ||
trailing: true | ||
}) { | ||
let timer; | ||
let lastTime = 0; | ||
function throttle(func, wait, options) { | ||
var timeout, context, args, result; | ||
var previous = 0; | ||
if (!options) options = {}; | ||
|
||
const later = function (context, args) { | ||
lastTime = options.trailing === true ? Date.now() : 0; | ||
func.apply(context, args); | ||
timer = null; | ||
var later = function () { | ||
previous = options.leading === false ? 0 : Date.now() || new Date().getTime(); | ||
timeout = null; | ||
result = func.apply(context, args); | ||
if (!timeout) context = args = null; | ||
}; | ||
|
||
let throttled = function () { | ||
let context = this; | ||
let args = arguments; | ||
let nowTime = Date.now(); | ||
if (!lastTime && options.leading === false) { | ||
lastTime = nowTime; //lastTime为0,会立即响应 | ||
} | ||
let remaining = wait - (nowTime - lastTime); | ||
if (remaining <= 0) { | ||
//remaining 的时间小于等于 0,表示两次触发的时间大于了间隔时间 | ||
if (timer) { | ||
//清楚定时器 | ||
clearTimeout(timer); | ||
timer = null; | ||
var throttled = function () { | ||
var now = Date.now() || new Date().getTime(); | ||
if (!previous && options.leading === false) previous = now; | ||
var remaining = wait - (now - previous); | ||
context = this; | ||
args = arguments; | ||
if (remaining <= 0 || remaining > wait) { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
lastTime = nowTime; | ||
func.apply(context, args); | ||
} else if (!timer && options.trailing) { | ||
// 倒计时还在进行中,响应新的事件,重新设置事件 | ||
timer = setTimeout(() => { later(context, args) }, remaining); | ||
previous = now; | ||
result = func.apply(context, args); | ||
if (!timeout) context = args = null; | ||
} else if (!timeout && options.trailing !== false) { | ||
timeout = setTimeout(later, remaining); | ||
} | ||
return result; | ||
}; | ||
|
||
throttled.cancel = function () { | ||
clearTimeout(timeout); | ||
lastTime = 0; | ||
timer = null; | ||
previous = 0; | ||
timeout = context = args = null; | ||
}; | ||
|
||
return throttled; | ||
|
@@ -81,12 +77,12 @@ | |
function a() { | ||
console.log(1); | ||
} | ||
// let b = throttle(a, 2000); | ||
let b = _.throttle(a, 2000); | ||
setTimeout(b, 0); | ||
setTimeout(b, 800); | ||
setTimeout(b, 1200); | ||
setTimeout(b, 2500); | ||
let b = throttle(a, 1000); | ||
// let b = _.throttle(a, 2000); | ||
// setTimeout(b, 0); | ||
setTimeout(b, 500); | ||
setTimeout(b, 1800); | ||
setTimeout(b, 2200); | ||
|
||
|
||
|
||
|