Created
February 15, 2019 10:47
-
-
Save vuthaihoc/fe966f03158ef48b8f704c01cb4fcc9a to your computer and use it in GitHub Desktop.
Web worker count down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<button onclick="startWorker()">Start Count</button> | |
<p>Count down <span id="number_view"></span></p> | |
<script> | |
var w; | |
var start_number = 10; | |
function startWorker() { | |
start_number = prompt("Count down from "); | |
if(typeof(Worker) !== "undefined") { | |
if(typeof(w) == "undefined") { | |
w = new Worker("ww.js"); | |
} | |
w.postMessage(start_number); | |
w.onmessage = function(event) { | |
document.getElementById("number_view").innerHTML = event.data; | |
}; | |
} else { | |
document.getElementById("number_view").innerHTML = "Sorry, your browser does not support Web Workers..."; | |
} | |
} | |
function stopWorker() { | |
w.terminate(); | |
w = undefined; | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i=10; | |
function timedCount() { | |
i=i-1; | |
if(i < 0){ | |
close(); | |
} | |
postMessage(i); | |
setTimeout("timedCount()", 500); | |
} | |
onmessage = function(e){ | |
i = e.data; | |
timedCount(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment