防反射JavaScript –如何讓您的JS等待

Debounce methods do not execute when invoked. Instead, they wait for a predetermined time before executing. If the same method is called again, the previous is cancelled and the timer restarts.

防抖動方法在調用時不執行。 而是,它們在執行之前等待預定的時間。 如果再次調用相同的方法,則前一個方法將被取消,計時器將重新啟動。

Here is a short video walk through in which I make a debounce method:

這是一段簡短的視頻,其中介紹了一種去抖動方法:

And here's the source code of the video tutorial:

這是視頻教程的源代碼:

Let's look at the code in more detail now.

讓我們現在更詳細地看一下代碼。

Assume you have a button called like this:

假設您有一個像這樣的按鈕:

<button id="myBtn">Click me</button>

And in your JS file you have something like this:

在您的JS文件中,您將得到以下內容:

document.getElementById('myBtn').addEventListener('click', () => {console.log('clicked');
})

Every time you click on your button, you would see a message in your console saying clicked.

每次單擊按鈕時,您的控制臺中都會顯示一條消息,提示您已clicked

Let's add a debounce method to our click event listener here:

讓我們在這里的click事件監聽器中添加一個反跳方法:

document.getElementById('myBtn').addEventListener('click', debouce(() => {console.log('click');
}, 2000))

The debounce method here takes in two arguments, callback & wait. callback is the function you want to execute, while wait is the configurable time period delay after which you want your callback to be executed.

這里的debounce方法接受兩個參數, callbackwaitcallback是要執行的功能,而wait是可配置的時間段延遲,在此延遲之后您要執行callback

Here our callback method simply is console.log('click'); and the wait is 2000 milliseconds.

在這里,我們的callback方法就是console.log('click'); wait2000 milliseconds

So given this debounce method, which takes in two parameters callback & wait, let's define debounce:

因此,給定這個debounce方法,它接受兩個參數callbackwait ,讓我們定義debounce

function debounce(callback, wait) {let timerId;return (...args) => {clearTimeout(timerId);timerId = setTimeout(() => {callback(...args);}, wait);};
}

Function debounce takes in two parameters: the callback (which is the function we want to execute) and the wait period (after how much delay we want to execute our callback).

函數debounce有兩個參數:回調(這是我們要執行的函數)和wait時間(在wait了多少延遲后才要執行回調)。

Inside the function, we simply return a function, which is the following:

在函數內部,我們僅返回一個函數,如下所示:

let timerId;
return (...args) => {clearTimeout(timerId);timerId = setTimeout(() => {callback(...args);}, wait);
};

What this function does is invoke our callback method after a certain period of time. And if during that period of time the same method is invoked again, the previous function is cancelled and the timer is reset and starts again.

該函數的作用是在一定時間后調用我們的callback方法。 如果在該時間段內再次調用相同的方法,則先前的功能將被取消,計時器將重置并重新啟動。

And that is it! All you need to know about what debounce is.

就是這樣! 您需要了解什么是去抖動。

Here is another bonus video on closures, because I used a closure inside my debounce function.

這是另一個關于閉包的獎勵視頻,因為我在debounce功能中使用了closure

Let me know on twitter if you were able to find the use of closure inside the debounce method.

如果您能夠在debounce方法中找到閉包的用法,請在Twitter上告訴我。

Happy coding, everyone.

祝大家編程愉快。

翻譯自: https://www.freecodecamp.org/news/debounce-javascript-tutorial-how-to-make-your-js-wait-up/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390260.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390260.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390260.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

21天學通Visual.Basic pdf

下載地址&#xff1a;網盤下載《21天學通Visual Basic(第2版)》是Visual Basic 6.0的入門教程&#xff0c;主要針對沒有程序設計基礎的讀者&#xff0c;詳細介紹了Visual Basic 6.0的基本概念和編程技術。書中通過大量的范例及綜合練習來介紹Visual Basic 6.0的基本概念、語言特…

297. 二叉樹的序列化與反序列化

297. 二叉樹的序列化與反序列化 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現…

交互式圖表_如何構建羅馬數字轉換器和交互式羅馬數字圖表

交互式圖表The Roman numerals are no longer an essential part of our daily lives. But we do use them when designing monuments, clocks, and even for sporting events.羅馬數字不再是我們日常生活中必不可少的部分。 但是我們在設計紀念碑&#xff0c;鐘表甚至體育賽事…

Python 08 面向對象

Python 面向對象 1、編程范式 2、面向對象特性 3、屬性、方法 4、三大特性 5、高級方法 6、類的特殊成員方法 7、反射 8、異常處理 9、單例模式 一、編程范式 編程&#xff1a;程序員用特定的語法數據結構算法組成的代碼來告訴計算機如何執行任務的過程 &#xff0c; 實現一個…

eclipse手動添加SVN插件

https://www.cnblogs.com/hcl1991/p/5888461.html 1.手動下載svn插件&#xff08;百度SVNsite-1.8.18&#xff09; 2.將下載好的SVNsite-1.8.18.zip 解壓 3.在eclipse安裝目錄的plugins新建SVN文件夾 4.將SVNsite-1.8.18解壓包下的features和plugins拷貝到新建的SVN文件夾下 5.…

440. 字典序的第K小數字

440. 字典序的第K小數字 給定整數 n 和 k&#xff0c;找到 1 到 n 中字典序第 k 小的數字。 注意&#xff1a;1 ≤ k ≤ n ≤ 109。 示例 : 輸入: n: 13 k: 2 輸出: 10 解釋: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]&#xff0c;所以第二小的數字是…

微信小程序 設置背景占滿整個頁面

微信小程序,默認的根節點是<page></page>&#xff0c;所有內容都包裹在這個標簽里&#xff0c;如何讓頁面的背景占滿整個屏幕&#xff1f;&#xff1f; <page><view class"bg">....</view> </page> .bg {background-image:ur…

udemy下載課程無法播放_最好的Udemy Web開發課程+熱門免費課程

udemy下載課程無法播放Heres a list of some of the most popular web development courses on Udemy:以下是Udemy上一些最受歡迎的Web開發課程的列表&#xff1a; Best General Web Development Courses on Udemy:關于Udemy的最佳常規Web開發課程&#xff1a; The Complete …

滲透測試初學者_滲透測試許可證:面向初學者的道德黑客課程

滲透測試初學者A penetration test is an authorized cyberattack on a computer system, performed to evaluate the security of the system. 滲透測試是對計算機系統的授權網絡攻擊&#xff0c;旨在評估系統的安全性。 Weve released a full pentesting course on the free…

Codeforces 915 E Physical Education Lessons

題目描述 This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is …

JDK 11 還有一個處于計劃階段的 JEP:讓其支持 TLS 1.3

開發四年只會寫業務代碼&#xff0c;分布式高并發都不會還做程序員&#xff1f; >>> JDK 11 最近有什么消息&#xff1f;我們不妨來看一下它的進展情況&#xff0c;包括最新的 JEP 提案。Java 的新版本發布計劃意味著總會有一款新的 JDK 即將推出。根據他們的計劃&a…

498. 對角線遍歷

498. 對角線遍歷 給定一個含有 M x N 個元素的矩陣&#xff08;M 行&#xff0c;N 列&#xff09;&#xff0c;請以對角線遍歷的順序返回這個矩陣中的所有元素&#xff0c;對角線遍歷如下圖所示。 示例: 輸入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 輸出: [1,2,4,7,5…

軟件測試測試用例編寫_不要先編寫所有軟件測試-只需編寫一個

軟件測試測試用例編寫Test Driven Development (TDD) is sometimes described as “writing tests first”. The TDD mantra states that we should not write code before we have written automated tests that exercise that code. Writing code first is considered subopt…

練習五

1.團隊模式和團隊的開發模式有什么關系&#xff1f; 答&#xff1a; 首先我來解釋一下這兩個名詞&#xff1a; 我查資料了解了一下&#xff0c;團隊模式&#xff0c;更偏向于多人合作的那種&#xff0c;而且我理解的“團隊”會是一種多人合作的情況下&#xff0c;長期磨合后的一…

Squid 訪問控制配置

Squid 訪問控制配置 主配置文件內加入限制參數 vim /etc/squid/squid.conf # 訪問控制 acl http proto HTTP # 限制訪問 good_domain添加兩個域名“.”表示半解析(相當于*) acl good_domain dstdomain .kevin.net .baidu.com # 允許good_domain內的域名訪問 http_access allow …

劍指 Offer 62. 圓圈中最后剩下的數字

0,1,,n-1這n個數字排成一個圓圈&#xff0c;從數字0開始&#xff0c;每次從這個圓圈里刪除第m個數字&#xff08;刪除后從下一個數字開始計數&#xff09;。求出這個圓圈里剩下的最后一個數字。 例如&#xff0c;0、1、2、3、4這5個數字組成一個圓圈&#xff0c;從數字0開始每…

rust 編程入門_面向初學者的Rust –最受歡迎的編程語言入門

rust 編程入門Rust has been voted Stack Overflow’s most loved programming language for five years in a row. This article will tell you why Rust is awesome.Rust已連續五年被評為Stack Overflow最受歡迎的編程語言。 本文將告訴您為什么Rust很棒。 Rust is a system…

【轉載】springboot:如何優雅的使用mybatis

這兩天啟動了一個新項目因為項目組成員一直都使用的是mybatis&#xff0c;雖然個人比較喜歡jpa這種極簡的模式&#xff0c;但是為了項目保持統一性技術選型還是定了 mybatis。到網上找了一下關于spring boot和mybatis組合的相關資料&#xff0c;各種各樣的形式都有&#xff0c;…

創建react應用程序_通過構建電影搜索應用程序在1小時內了解React

創建react應用程序If youve been meaning to learn React but are unsure of where to start, Scrimbas brand new Build a Movie Search App course is perfect for you! 如果您一直想學習React&#xff0c;但是不確定從哪里開始&#xff0c;那么Scrimba全新的Build a Movie S…

GeoServer自動發布地圖服務

1 NetCDF氣象文件自動發布案例 GeoServer是一個地理服務器&#xff0c;提供了管理頁面進行服務發布&#xff0c;樣式&#xff0c;切片&#xff0c;圖層預覽等一系列操作&#xff0c;但是手動進行頁面配置有時并不滿足業務需求&#xff0c;所以GeoServer同時提供了豐富的rest接口…