I want to make this code infinite loop (i placed it in loop.js) without crashing the browser with a really short delay
player.style.right = (Number(player.style.right.split("px")[0]) - dx) "px";
player.style.top = (Number(player.style.right.split("px")[0]) - dy) "px";
if (player.style.right.split("px")[0] > 315) {
player.style.right = 315 "px";
}
if (player.style.right.split("px")[0] < -30) {
player.style.right = -30 "px";
}
if (player.style.top.split("px")[0] < 0) {
player.style.top = 0 "px";
}
if (player.style.top.split("px")[0] > 282) {
player.style.top = 282 "px";
}
console.log(`dx:${dx}`)
console.log(`dy:${dy}`);
How can i do it?
CodePudding user response:
You can't do an infinite loop directly.
But you can use setInterval
to not block the main thread entirely.