dom/workers/test/threadTimeouts_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     5 var gTimeoutId;
     6 var gTimeoutCount = 0;
     7 var gIntervalCount = 0;
     9 function timeoutFunc() {
    10   if (++gTimeoutCount > 1) {
    11     throw new Error("Timeout called more than once!");
    12   }
    13   postMessage("timeoutFinished");
    14 }
    16 function intervalFunc() {
    17   if (++gIntervalCount == 2) {
    18     postMessage("intervalFinished");
    19   }
    20 }
    22 function messageListener(event) {
    23   switch (event.data) {
    24     case "startTimeout":
    25       gTimeoutId = setTimeout(timeoutFunc, 2000);
    26       clearTimeout(gTimeoutId);
    27       gTimeoutId = setTimeout(timeoutFunc, 2000);
    28       break;
    29     case "startInterval":
    30       gTimeoutId = setInterval(intervalFunc, 2000);
    31       break;
    32     case "cancelInterval":
    33       clearInterval(gTimeoutId);
    34       postMessage("intervalCanceled");
    35       break;
    36     case "startExpression":
    37       setTimeout("this.postMessage('expressionFinished');", 2000);
    38       break;
    39     default:
    40       throw "Bad message: " + event.data;
    41   }
    42 }
    44 addEventListener("message", messageListener, false);

mercurial