Image stabilization and a throttle to test the

Shake throttling could not tell silly concept, always confused, usually better, when you want to use google you will understand, but if the interview were reversed, it would be dead, for the simple knowledge , be sure to hold the score.

Throttling

When the event to keep firing, to ensure a certain event handler is called only once period of time (faith view of life to cut expenditure, although I visit every day a treasure, but limit yourself to only buy one a month, which was throttling)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>节流</title>
</head>
<body>
<div id="app" style="height:600px"></div>
<script>
    window.onload = function() {
       function testFn() {
           console.log ( ' trigger ' )
       }
       function commonFn(value) {
           let time = null
           return function() {
               if (!time) {
                    time = setTimeout(() => {
                        testFn()
                        time = null;
                    }, value);
               }
                
           }
       }
       window.addEventListener('scroll', commonFn(2000))
    }
</script>
</body>
</html>

 

Shake

When the event to keep firing certain period of time no longer trigger event, the event handler will be executed once, if before the set time comes, once again triggered the event is restarted

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>抖动</title>
</head>
<body>
<div id="app" style="height:1000px"></div>
<script>
    window.onload = function() {
        window.addEventListener('scroll', function() {
            let time = null
            return function(res) {
                clearTimeout(time)
                time = setTimeout(() => {
                    console.log(245)
                }, 1000)
            }
        }());
    }
</script>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/Tiboo/p/11795788.html