diff --git a/files/en-us/web/api/windoworworkerglobalscope/settimeout/index.html b/files/en-us/web/api/windoworworkerglobalscope/settimeout/index.html index 9a03a8044902f35..d47693724f1d877 100644 --- a/files/en-us/web/api/windoworworkerglobalscope/settimeout/index.html +++ b/files/en-us/web/api/windoworworkerglobalscope/settimeout/index.html @@ -87,7 +87,7 @@
const myArray = ['zero', 'one', 'two']; myArray.myMethod = function (sProperty) { - alert(arguments.length > 0 ? this[sProperty] : this); + console.log(arguments.length > 0 ? this[sProperty] : this); }; myArray.myMethod(); // prints "zero,one,two" @@ -153,12 +153,12 @@Passing string literals
// Don't do this -setTimeout("alert('Hello World!');", 500); +setTimeout("console.log('Hello World!');", 500);// Do this instead setTimeout(function() { - alert('Hello World!'); + console.log('Hello World!'); }, 500);@@ -303,30 +303,43 @@Setting and clearing timeouts
The following example sets up two simple buttons in a web page and hooks them to the
setTimeout()
andclearTimeout()
routines. Pressing the first - button will set a timeout which calls an alert dialog after two seconds and stores the + button will set a timeout which shows a message after two seconds and stores the timeout id for use byclearTimeout()
. You may optionally cancel this timeout by pressing on the second button.HTML
-<button onclick="delayedAlert();">Show an alert box after two seconds</button> -<button onclick="clearAlert();">Cancel alert before it happens</button> +<button onclick="delayedMessage();">Show an message after two seconds</button> +<button onclick="clearMessage();">Cancel message before it happens</button> + +<div id="output"></div>JavaScript
let timeoutID; -function delayedAlert() { - timeoutID = setTimeout(window.alert, 2*1000, 'That was really slow!'); +function setOutput(outputContent) { + document.querySelector('#output').textContent = outputContent; +} + +function delayedMessage() { + setOutput(''); + timeoutID = setTimeout(setOutput, 2*1000, 'That was really slow!'); } -function clearAlert() { +function clearMessage() { clearTimeout(timeoutID); }+ +Result
{{EmbedLiveSample('setting_and_clearing_timeouts')}}