JavaScript BOM- objects

Top-level object window

Common load event

  • onload when the document is fully loaded trigger (including images, script files, CSS files, etc.); traditional registration method can only write one more subject to the last; there is no limit if you use addEventListener
  • When DOMContentLoaded event triggered only when the DOM is ready, not including style sheets, images, flash and so on.
<Script> // first window .onload = function () {
         Alert ( '111') 
    }; window .addEventListener ( 'Load', function () {
         Alert ( '222') 
    }); // do not affect the document .addEventListener ( 'the DOMContentLoaded', function () {
         Alert (33 is); // not loaded images, etc. 
    }) 
</ Script>

    
    
    

    

Sample Code
Timer
  1. setTimeout () is executed only once
  2. setInterval () repeated

setTimeout () Timer

The timer is turned

  • the window.setTimeout (calling function, [milliseconds delay])
<script>
    function callback() {
        console.log('111');
    }

    var tinner1 = setTimeout(callback, 2000);
    var tinner2 = setTimeout(callback, 3000);

</script>
Sample Code
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <img src="ad.jpg" alt="" class="ad">
    <script>
        var img = document.querySelector('.ad');
        setTimeout(function () {
            img.style.display = 'none';
        },2000)

    </script>
</body>

</html>
Case - Close Ad

Stop the timer

  • clearTimeout(TimeoutID)
<button>关闭定时器</button>
<script>
    var timer = setTimeout(function () {
        console.log('111');
    }, 1000);

    var btn = document.querySelector('button');
    btn.addEventListener('click', function () {
        clearTimeout(timer)
    })

</script>
Sample Code

setInterval () Timer

The timer is turned

  • the window.setInterval (calling function, [milliseconds delay])

Stop the timer

  • clearInterval(TimeoutID)
<button> Off Timer </ Button> 
<Script> var Timer = the setInterval ( function () { 
        the console.log (111); 
    }, 1000); var BTN = Document .querySelector ( 'Button'); 
    btn.onclick = function () {
         the clearInterval (timer) // off-timer 
    } 
</ Script>
    
    
Sample Code

note:

  • The first execution after interval milliseconds, all the best package to perform a function, to prevent the problem began to refresh the page blank

Guess you like

Origin www.cnblogs.com/py-web/p/12505453.html