From 2325c66d69767074573d82ca8354173e2bd51921 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Wed, 21 Jul 2021 19:17:00 -0700 Subject: [PATCH] Fix double execution in Code demo (#5152) * Fix double execution in Code demo PR #5037 fixed double execution of the run button on mobile. But the link button and other events still suffer from double execution. This PR moves the fix from the run button to the central bind function so that it applies universally. * Pass the event to the user function Not used but good to have. --- demos/code/code.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/demos/code/code.js b/demos/code/code.js index 2377203a2..5a33f406c 100644 --- a/demos/code/code.js +++ b/demos/code/code.js @@ -188,7 +188,12 @@ Code.bindClick = function(el, func) { el = document.getElementById(el); } el.addEventListener('click', func, true); - el.addEventListener('touchend', func, true); + function touchFunc(e) { + // Prevent code from being executed twice on touchscreens. + e.preventDefault(); + func(e); + } + el.addEventListener('touchend', touchFunc, true); }; /** @@ -549,14 +554,8 @@ Code.initLanguage = function() { /** * Execute the user's code. * Just a quick and dirty eval. Catch infinite loops. - * @param {Event} event Event created from listener bound to the function. */ -Code.runJS = function(event) { - // Prevent code from being executed twice on touchscreens. - if (event.type == 'touchend') { - event.preventDefault(); - } - +Code.runJS = function() { Blockly.JavaScript.INFINITE_LOOP_TRAP = 'checkTimeout();\n'; var timeouts = 0; var checkTimeout = function() {