Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

62
LINES

< > BotCompany Repo | #1013795 // Auto-Restarting Web Worker Test

JavaX module [tags: ai1-lol use-pretranspiled] - homepage

Download Jar. Libraryless. Click here for Pure Java version (1856L/12K/43K).

1  
!7
2  
3  
html {
4  
  if (eq(uri, "/worker.js"))
5  
    ret [[
6  
      addEventListener('message', function(e) {
7  
        var startValue = e.data, i = startValue;
8  
        postMessage("WORKING!");
9  
        var startTime = new Date();
10  
        while (true) {
11  
          i = i+1;
12  
          if ((i % 1000000) == 0) {
13  
            var ms = new Date()-startTime;
14  
            var mips = ((i-startValue)/ms/1000).toFixed(2);
15  
            postMessage("Counted to: " + i + " in " + ms + " ms ("
16  
              + mips + " million steps/s)");
17  
            if (ms >= 4000) {
18  
              postMessage(["continue", i]);
19  
              break;
20  
            }
21  
          }
22  
        }
23  
      });
24  
    ]];
25  
    
26  
  ret htitle("Web Worker Test")
27  
    + hbody(
28  
      p("Count numbers: " + hfulltag("output", "", id := "result"))
29  
    + p(hbutton("Restart Worker", onClick := "restart()"))
30  
    + p(hbutton("Dummy Button"))
31  
  + hjavascript([[
32  
    var worker;
33  
    
34  
    function restart() {
35  
      worker.terminate();
36  
      go();
37  
    }
38  
    
39  
    function go() {
40  
      cont(0);
41  
    }
42  
    
43  
    function cont(i) {
44  
      if (typeof(Worker) === "undefined") {
45  
        document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
46  
        return;
47  
      }
48  
      document.getElementById("result").innerHTML = "Making web worker";
49  
      worker = new Worker($LINK);
50  
      worker.onmessage = function(event) {
51  
        var e = event.data;
52  
        if (typeof(e) == "object" && e[0] == 'continue') {
53  
          worker.terminate();
54  
          //setTimeout(function() { cont(e[1]) }, 1000);
55  
          cont(e[1]);
56  
        } else
57  
          document.getElementById("result").innerHTML = event.data;
58  
      };
59  
      worker.postMessage(i);
60  
    }
61  
  ]]).replace("$LINK", jsQuote(relativeRawBotLink(programID(), "worker.js"))), onLoad := "go()");
62  
}

Author comment

Began life as a copy of #1013783

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013795
Snippet name: Auto-Restarting Web Worker Test
Eternal ID of this version: #1013795/8
Text MD5: 2bf7e8160cebb4566dd5604c82877feb
Transpilation MD5: d277ea8875588978b3176ccf0a9931cb
Author: stefan
Category: javax / web workers
Type: JavaX module
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-02-28 13:35:07
Source code size: 1862 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 513 / 1063
Version history: 7 change(s)
Referenced in: [show references]