Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Anti-shaking and throttling in js

Shulou Source: shulou.com Published: 2022-06-01 06:11:43 09月24日 Update

This article is to share with you about how js can achieve anti-shake and cost-saving. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Anti-shaking & throttling

Anti-shaking and throttling are high-level skills, and the most common occasion in the business is to search for content change tips. Even if you don't add it, you may not be able to see the difference, but adding novice maintenance code may worship you.

Anti-shaking

Function debounce (func, wait) {let timeout; return function () {let context = this; let args = arguments; if (timeout) clearTimeout (timeout); timeout = setTimeout (() = > {func.apply (context, args)}, wait);}}

Throttling

Function throttle (func, wait) {let previous = 0; return function () {let now = Date.now (); let context = this; let args = arguments; if (now-previous > wait) {func.apply (context, args); previous = now;}} Thank you for reading! This is the end of the article on "how to achieve anti-shaking and cost-saving in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Content more articles good practical business that is code information situations skills articles novices see knowledge high-level see reference help tips Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info MariaDB MySQL Shulou Technology