Last active
October 31, 2020 03:07
-
-
Save yoshinari-nomura/912b4d77b052c3b6c48147180ee61720 to your computer and use it in GitHub Desktop.
Infinite loop in JavaScript event handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<canvas id="canvas" width="300" height="300"></canvas> | |
<script> | |
let canvas = document.getElementById('canvas'); | |
let ctx = canvas.getContext('2d'); | |
ctx.strokeRect(0, 0, 300, 300); | |
canvas.addEventListener('click', ev => handle_click(ev)); | |
function handle_click(ev) { | |
console.log("click!"); | |
ctx.strokeRect(ev.offsetX, ev.offsetY, 10, 10); // ← (A) | |
// Enabling these lines, no rect (A) is shown. | |
// I expect only the first one rect is drawn? | |
// | |
// while (true) { | |
// console.log("!"); | |
// } | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment