1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/test/threadTimeouts_worker.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,44 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 +var gTimeoutId; 1.9 +var gTimeoutCount = 0; 1.10 +var gIntervalCount = 0; 1.11 + 1.12 +function timeoutFunc() { 1.13 + if (++gTimeoutCount > 1) { 1.14 + throw new Error("Timeout called more than once!"); 1.15 + } 1.16 + postMessage("timeoutFinished"); 1.17 +} 1.18 + 1.19 +function intervalFunc() { 1.20 + if (++gIntervalCount == 2) { 1.21 + postMessage("intervalFinished"); 1.22 + } 1.23 +} 1.24 + 1.25 +function messageListener(event) { 1.26 + switch (event.data) { 1.27 + case "startTimeout": 1.28 + gTimeoutId = setTimeout(timeoutFunc, 2000); 1.29 + clearTimeout(gTimeoutId); 1.30 + gTimeoutId = setTimeout(timeoutFunc, 2000); 1.31 + break; 1.32 + case "startInterval": 1.33 + gTimeoutId = setInterval(intervalFunc, 2000); 1.34 + break; 1.35 + case "cancelInterval": 1.36 + clearInterval(gTimeoutId); 1.37 + postMessage("intervalCanceled"); 1.38 + break; 1.39 + case "startExpression": 1.40 + setTimeout("this.postMessage('expressionFinished');", 2000); 1.41 + break; 1.42 + default: 1.43 + throw "Bad message: " + event.data; 1.44 + } 1.45 +} 1.46 + 1.47 +addEventListener("message", messageListener, false);