123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <button id="btn">按钮A</button>
- <button id="btnB">按钮B</button>
- <button id="stop">停止</button>
- <button id="btnC">按钮C</button>
-
- <script>
- var btn = document.getElementById('btn')
- var btnB = document.getElementById('btnB')
- var stop = document.getElementById('stop')
- var btnC = document.getElementById('btnC')
- btn.onclick = function(){
- var a = 1
- var b = setTimeout(function(){
- a++
- console.log(a)
- },1000)
- }
- btnB.onclick = function(){
- var a = 1
- var c = setInterval(function(){
- a++
- console.log(a)
- },1000)
- stop.onclick = function(){
- clearInterval(c)
- }
- }
- //setTimeout 只执行一次
- //setInterval 不断的执行
- btnC.onclick = function(){
- //实现随机数 0-1
- var a = Math.random() * 100
- //Math.floor() 向下取整 跟四舍五入没有关系
- console.log(Math.floor(a))
-
- }
- </script>
- </body>
- </html>
|