我正在开发一个小游戏,它会计算你在 30 秒内点击图像的次数。我有 2 个单独的函数,一个函数为图像生成一个新位置并更新玩家分数,另一个函数从 30 秒开始倒计时并更新“已点击”段落。问题是我似乎无法同时运行 2 个函数我做了一些研究,我看到 js 只能运行一个线程,那么这怎么可能呢?<html><head></head><body> <img id='ball' src="ball.png" alt="Logo"style="position: absolute; top: 450px; left: 900; width:70px;height:70px;" onclick="moveBall()"> <p id="demo"></p> <p id="timer"></p> <p id="Clicks" style="font-family: Impact, Charcoal, sans-serif; font-size:150px; color:brown;">0</p> <script> function moveBall() { var ball = document.getElementById("ball"); ball.style.left = Math.floor(Math.random() * 1500); ball.style.top = Math.floor(Math.random() * 900) document.getElementById('Clicks').innerHTML = parseInt(document.getElementById('Clicks').innerHTML) + 1; if(document.getElementById('Clicks').innerHTML == 20) { document.body.style.backgroundColor = "#68a2ff"; document.getElementById('Clicks').style.color = '#ff688d'; } } function sleep() { var start = new Date().getTime(); var finish = start + 20; while(finish - start != 0) { document.getElementById('timer').innerHTML = finish - start; } } </script></body></html>
添加回答
举报
0/150
提交
取消