Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement debounce with JavaScript

Shulou Source: shulou.com Published: 2022-06-01 03:45:36 09月22日 Update

This article mainly introduces JavaScript how to achieve debounce, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Debounce (anti-shaking)

The function will only be executed once within n seconds after triggering the high frequency time, and if the high frequency time is triggered again within n seconds, the time will be recalculated.

Const debounce = (fn, time) = > {let timeout = null; return function () {clearTimeout (timeout) timeout = setTimeout (() = > {fn.apply (this, arguments);}, time);}}

Anti-dithering is often used for users to save request resources for search input. Anti-dithering is only triggered once when window triggers resize events.

Thank you for reading this article carefully. I hope the article "how to achieve debounce in JavaScript" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Articles time events value interest again function at the same time more friends users knowledge editing industry resources information information channels channels references learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Shulou Technology MySQL vpn