Skip to content

Instantly share code, notes, and snippets.

@yoshinari-nomura
Last active October 31, 2020 03:07
Show Gist options
  • Save yoshinari-nomura/912b4d77b052c3b6c48147180ee61720 to your computer and use it in GitHub Desktop.
Save yoshinari-nomura/912b4d77b052c3b6c48147180ee61720 to your computer and use it in GitHub Desktop.
Infinite loop in JavaScript event handler
<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