principleThe principle of anti-shake is: you can trigger an event, but I must execute it n seconds after the event is triggered. If you trigger this event again within n seconds of an event being triggered, I will use the time of the new event as the basis and execute it n seconds later. In short, I will not execute until you trigger the event and no more events are triggered within n seconds. Case<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"> <title>debounce</title> <style> * { margin: 0; padding: 0; } #container { width: 100%; height: 200px; line-height: 200px; text-align: center; color: #fff; background-color: #444; font-size: 30px; } </style> </head> <body> <div id="container"></div> <script src="debounce.js"></script> </body> </html> function getUserAction(e) { console.log(this); console.log(e); container.innerHTML = count++; }; function debounce(func, wait) { var timeout; return function () { clearTimeout(timeout); timeout = setTimeout(() => { func.apply(this, arguments); // Solve this and event object event }, wait); } } container.onmousemove = debounce(getUserAction, 1000); This is the end of this article about JavaScript anti-shake case study. For more relevant JavaScript anti-shake content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: mysql command line script execution example
>>: MySQL controls the number of attempts to enter incorrect passwords
To achieve the plus sign effect shown below: To a...
1. MySQL transaction concept MySQL transactions a...
1. Download MySQL Official website download addre...
Table of contents How to create a Pod? kubectl to...
This article records the installation and configu...
I am writing a small program recently. Because th...
In the process of learning CSS3, I found that man...
For a newly created website, take ASP.NET MVC5 as...
Download opencv.zip Install the dependencies ahea...
Table of contents 1. Docker enables remote access...
I often need to change nginx configuration at wor...
The environment of this article is Windows 10, an...
This article shares two methods of implementing t...
Preface The Linux system is controlled by the sys...
MySQL database tables can create, view, rebuild a...