Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | var gTimeoutId; |
michael@0 | 6 | var gTimeoutCount = 0; |
michael@0 | 7 | var gIntervalCount = 0; |
michael@0 | 8 | |
michael@0 | 9 | function timeoutFunc() { |
michael@0 | 10 | if (++gTimeoutCount > 1) { |
michael@0 | 11 | throw new Error("Timeout called more than once!"); |
michael@0 | 12 | } |
michael@0 | 13 | postMessage("timeoutFinished"); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | function intervalFunc() { |
michael@0 | 17 | if (++gIntervalCount == 2) { |
michael@0 | 18 | postMessage("intervalFinished"); |
michael@0 | 19 | } |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function messageListener(event) { |
michael@0 | 23 | switch (event.data) { |
michael@0 | 24 | case "startTimeout": |
michael@0 | 25 | gTimeoutId = setTimeout(timeoutFunc, 2000); |
michael@0 | 26 | clearTimeout(gTimeoutId); |
michael@0 | 27 | gTimeoutId = setTimeout(timeoutFunc, 2000); |
michael@0 | 28 | break; |
michael@0 | 29 | case "startInterval": |
michael@0 | 30 | gTimeoutId = setInterval(intervalFunc, 2000); |
michael@0 | 31 | break; |
michael@0 | 32 | case "cancelInterval": |
michael@0 | 33 | clearInterval(gTimeoutId); |
michael@0 | 34 | postMessage("intervalCanceled"); |
michael@0 | 35 | break; |
michael@0 | 36 | case "startExpression": |
michael@0 | 37 | setTimeout("this.postMessage('expressionFinished');", 2000); |
michael@0 | 38 | break; |
michael@0 | 39 | default: |
michael@0 | 40 | throw "Bad message: " + event.data; |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | addEventListener("message", messageListener, false); |