12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <!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">按钮</button>
- <button id="stop">按钮B</button>
- <button id="num">按钮C</button>
- <script>
- var btn = document.getElementById('btn')
- var stop = document.getElementById('stop')
- var num = document.getElementById('num')
- /* 间隔多少毫秒去执行 */
- btn.onclick = function () {
- var b = 1
- var a = setInterval(function () {
- if (b < 10) {
- b++
- console.log(b)
- } else {
- clearInterval(a)
- }
- }, 1000)
- }
- stop.onclick = function () {
- var c = setTimeout(function () {
- console.log(111)
- }, 1000)
- }
- /*
- setInterval 不断的去执行 通过clearInterval 清除定时器
- setTimeout 只执行一次 */
- num.onclick = function () {
- //获取一个随机数
- //Math.random() 0~1
- //Math.floor 向下取整 跟四舍五入没关系
- var h = setInterval(function () {
- var nn = Math.random() * 10
- var mm = Math.floor(nn)
- console.log(mm)
- }, 1000)
- }
- </script>
- </body>
- </html>
|