!7
html {
if (eq(uri, "/worker.js"))
ret [[
postMessage("WORKING! Watch your CPU.");
var startTime = new Date();
var start = 0, i = start;
while (true) {
for (j = 0; j < 1000; j++) {}
i = i+1;
if ((i % 1000) == 0) {
var ms = new Date()-startTime;
var mips = ((i-start)/ms).toFixed(2);
postMessage("Counted to: " + i + " in " + ms + " ms ("
+ mips + " million steps/s)");
}
}
]];
ret htitle("Web Worker Test")
+ hbody(
p("Count numbers: " + hfulltag("output", "", id := "result"))
+ p(hbutton("Restart Worker", onClick := "restart()"))
+ hjavascript([[
var worker;
function restart() {
worker.terminate();
go();
}
function go() {
if (typeof(Worker) === "undefined") {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
return;
}
document.getElementById("result").innerHTML = "Making web worker";
worker = new Worker($LINK);
worker.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
//document.getElementById("result").innerHTML = "Worker made, starting";
//worker.postMessage();
}
]]).replace("$LINK", jsQuote(relativeRawBotLink(programID(), "worker.js"))), onLoad := "go()");
}