長(zhǎng)沙網(wǎng)站seo技巧今日足球賽事數(shù)據(jù)
當(dāng)使用lodash的throttle函數(shù)時(shí)會(huì)觸發(fā)兩次,分別在最開始和最后。
嚴(yán)格來說不算是bug,因?yàn)楣俜轿臋n寫的很清楚。throttle函數(shù)其實(shí)有三個(gè)參數(shù):
_.throttle(func, [wait=0], [options=])
func
: 要節(jié)流的函數(shù)
wait
: 等待時(shí)間
options
: 選項(xiàng)
options.leading=true
(boolean): 指定調(diào)用在節(jié)流開始前,也就是第一次點(diǎn)擊。
options.trailing=true
(boolean): 指定調(diào)用在節(jié)流結(jié)束后,也就是最后一次點(diǎn)擊。
options的默認(rèn)值為:{leading: true, trailing: true}
所以其實(shí)throttle函數(shù)默認(rèn)就是會(huì)調(diào)用兩次。分別是第一次和最后一次。
如果想要throttle函數(shù)只會(huì)調(diào)用一次,可以設(shè)置options.trailing=false。這樣函數(shù)的表現(xiàn)就像普通的截流函數(shù)了。
// 點(diǎn)擊后就調(diào)用 `renewToken`,但5分鐘內(nèi)超過1次。
var throttled = _.throttle(renewToken, 300000, { 'trailing': false });