Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of function throttling and Anti-shaking in js

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the js function throttling and anti-shake example analysis, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Throttling and Anti-shaking of js

Function anti-shake means that the callback is performed after the event is triggered n seconds, and if the event is triggered again within n seconds, the time is re-timed. This can be used on some click request events to avoid sending multiple requests to the backend because of multiple clicks by the user.

Function throttling means to specify a unit time in which only one callback function that triggers an event can be executed. If an event is triggered multiple times in the same unit time, it will only take effect once. Throttling can be used in the event listening of the scroll function to reduce the frequency of event calls through event throttling.

/ / implementation of anti-shake function function debounce (fn, wait) {var timer = null; return function () {var context = this, args = arguments; / / if there is a timer at this time, cancel the previous timer if (timer) {clearTimeout (timer); timer = null } / / set timer to execute timer = setTimeout (() = > {fn.apply (context, args);}, wait);};} / / function throttling implementation after the event interval is specified; function throttle (fn, delay) {var preTime = Date.now (); return function () {var context = this, args = arguments, nowTime = Date.now () / / if the interval exceeds the specified time, the function is executed. If (nowTime-preTime > = delay) {preTime = Date.now (); return fn.apply (context, args);}};} above is all the contents of this article entitled "example Analysis of function throttling and Anti-shaking in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report