gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
纠正这段代码——<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta…
- User
纠正这段代码——
<script> var btn=document.getElementById("btn"); var showNum=document.getElementById("showNum"); btn.onclick=function(){ if(btn.innerHTML=="开始摇号"){ for (let i=0;i<10000;i++){ setInterval(function(){ var num=Math.ceil(Math.random()*37+1); showNum.innerHTML=num; },200) } btn.innerHTML=="停止摇号"; }else{ btn.innerHTML="开始摇号" clearInterval(timer1); } } </script> - Assistant
请看下面是修正后的代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; box-sizing: border-box; } div{ width: 400px; /* 将"widows"改为"width" */ height: 400px; background-color: gray; padding:30px; } div p{ font-size: 30px; color: red; text-align:center; } div button{ width: 100px; height: 40px; line-height: 40px; text-align: center; position: absolute; left: calc(50% -50px); top: 200px; } </style> </head> <body> <div> <p id="showNum">0</p> <button id="btn">开始摇号</button> </div> <script> var btn=document.getElementById("btn"); var showNum=document.getElementById("showNum"); var timer1; /* 声明一个timer1的变量 */ btn.onclick=function(){ if(btn.innerHTML=="开始摇号"){ timer1 = setInterval(function(){ /* 将setInterval放到循环外面 */ var num=Math.ceil(Math.random()*37+1); showNum.innerHTML=num; },200); btn.innerHTML="停止摇号"; /* 将"=="改为"=" */ }else{ btn.innerHTML="开始摇号"; /* 将"=="改为"=" */ clearInterval(timer1); } } </script> </body> </html>