国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

節(jié)流與防抖

2022-10-18 09:40 作者:對(duì)白淺淺  | 我要投稿

<!DOCTYPE html>

<html lang="en">

<head>

? ? <meta charset="UTF-8">

? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>Document</title>

? ? <style>

? ? ? ? html,body{

? ? ? ? ? ? height: 500%;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <button id="button">

? ? ? ? 按鈕

? ? </button>

? ? <!-- 節(jié)流與防抖 -->

? ? <script>

? ? ? ? // 節(jié)流:一個(gè)函數(shù)執(zhí)行一次后,只有大于設(shè)定的執(zhí)行周期才會(huì)執(zhí)行第二次

? ? ? ? function throttle(fn,delay){

? ? ? ? ? ? let lastTime = 0

? ? ? ? ? ? return function(){

? ? ? ? ? ? ? ? let nowTime = Date.now()

? ? ? ? ? ? ? ? if(nowTime - lastTime > delay){

? ? ? ? ? ? ? ? ? ? fn.call(this) ?//修正this指向問題

? ? ? ? ? ? ? ? ? ? lastTime = nowTime

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? document.onscroll=throttle(function(){

? ? ? ? ? ? console.log("scroll 時(shí)間被觸發(fā)了"+ Date.now())

? ? ? ? },2000)

? ? ? ?


? ? ? ? //防抖:一個(gè)需要頻繁觸發(fā)的函數(shù),在規(guī)定時(shí)間內(nèi)只讓最后一次生效,前面的不生效

? ? ? ? function debounce(fn,delay){

? ? ? ? ? ? let timer =null

? ? ? ? ? ? return function(){

? ? ? ? ? ? ? ? clearTimeout(timer)

? ? ? ? ? ? ? ? timer = setTimeout(function(){

? ? ? ? ? ? ? ? ? ? fn.apply(this)

? ? ? ? ? ? ? ? },delay)

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? document.getElementById("button").onclick = debounce(function(){

? ? ? ? ? ? console.log("點(diǎn)擊事件被觸發(fā)了"+Date.now())

? ? ? ? },500)


? ? ? ? // 節(jié)流進(jìn)階

? ? ? ? function debounce2(fn,delay,immediate){ ?//immediate為是否先執(zhí)行一次節(jié)流

? ? ? ? ? ? let timer=null

? ? ? ? ? ? return function(){

? ? ? ? ? ? ? ? clearTimeout(timer)

? ? ? ? ? ? ? ? if(immediate){

? ? ? ? ? ? ? ? ? ? let doOnce = !timer

? ? ? ? ? ? ? ? ? ? timer = setTimeout(function(){

? ? ? ? ? ? ? ? ? ? ? ? timer=null

? ? ? ? ? ? ? ? ? ? },delay)

? ? ? ? ? ? ? ? ? ? if(doOnce){

? ? ? ? ? ? ? ? ? ? ? ? fn.apply(this)

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? // 如果沒有設(shè)置第三個(gè)參數(shù),就是什么時(shí)候停止操作,之后delay時(shí)間才執(zhí)行

? ? ? ? ? ? ? ? ? ? timer = setTimeout(function(){

? ? ? ? ? ? ? ? ? ? ? ? fn.apply(this)

? ? ? ? ? ? ? ? ? ? },delay)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? </script>

? ?

</body>

</html>


節(jié)流與防抖的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
东莞市| 云梦县| 共和县| 兴隆县| 伊吾县| 南汇区| 黎平县| 正阳县| 望江县| 丹东市| 溧水县| 璧山县| 沾化县| 原平市| 龙江县| 吴桥县| 灵丘县| 高安市| 浑源县| 衢州市| 台湾省| 台东县| 乳源| 太保市| 夏河县| 马关县| 苍山县| 彰化市| 开江县| 梨树县| 前郭尔| 斗六市| 汉沽区| 景洪市| 巧家县| 龙里县| 白沙| 波密县| 阿拉善左旗| 青龙| 玉树县|