JavaScript anti-shake case study

JavaScript anti-shake case study

principle

The 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:
  • Implementation and usage scenarios of JS anti-shake throttling function
  • JavaScript deshaking and throttling examples
  • How to understand JS function anti-shake and function throttling
  • Javascript throttle function and debounce function
  • Strange problems encountered and solutions for JavaScript anti-shake and throttling
  • How to hand-write javascript throttling and deshaking functions in an interview
  • Application scenarios of js throttling and anti-shake, and specific implementation of throttling and anti-shake in vue
  • A brief discussion on JavaScript throttling and anti-shake functions

<<:  mysql command line script execution example

>>:  MySQL controls the number of attempts to enter incorrect passwords

Recommend

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

Detailed explanation of Mysql transaction processing

1. MySQL transaction concept MySQL transactions a...

MySQL 8.x msi version installation tutorial with pictures and text

1. Download MySQL Official website download addre...

How to create a Pod in Kubernetes

Table of contents How to create a Pod? kubectl to...

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

CSS3 animation to achieve the effect of streamer button

In the process of learning CSS3, I found that man...

Introduction to ApplicationHost.config (IIS storage configuration area file)

For a newly created website, take ASP.NET MVC5 as...

Solution for installing opencv 3.2.0 in Ubuntu 18.04

Download opencv.zip Install the dependencies ahea...

Intellij IDEA quick implementation of Docker image deployment method steps

Table of contents 1. Docker enables remote access...

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

JavaScript to achieve fancy carousel effect

This article shares two methods of implementing t...

How to use crontab to add scheduled tasks in Linux

Preface The Linux system is controlled by the sys...

Introduction to using MySQL commands to create, delete, and query indexes

MySQL database tables can create, view, rebuild a...