!7
html {
if (eq(uri, "/worker.js"))
ret [[
addEventListener('message', function(e) {
var startValue = e.data, i = startValue;
postMessage("WORKING!");
var startTime = new Date();
while (true) {
i = i+1;
if ((i % 1000000) == 0) {
var ms = new Date()-startTime;
var mips = ((i-startValue)/ms/1000).toFixed(2);
postMessage("Counted to: " + i + " in " + ms + " ms ("
+ mips + " million steps/s)");
if (ms >= 4000) {
postMessage(["continue", i]);
break;
}
}
}
});
]];
ret htitle("Web Worker Test")
+ hbody(
p("Count numbers: " + hfulltag("output", "", id := "result"))
+ p(hbutton("Restart Worker", onClick := "restart()"))
+ p(hbutton("Dummy Button"))
+ hjavascript([[
var worker;
function restart() {
worker.terminate();
go();
}
function go() {
cont(0);
}
function cont(i) {
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) {
var e = event.data;
if (typeof(e) == "object" && e[0] == 'continue') {
worker.terminate();
//setTimeout(function() { cont(e[1]) }, 1000);
cont(e[1]);
} else
document.getElementById("result").innerHTML = event.data;
};
worker.postMessage(i);
}
]]).replace("$LINK", jsQuote(relativeRawBotLink(programID(), "worker.js"))), onLoad := "go()");
}