-
Notifications
You must be signed in to change notification settings - Fork 18
/
Example-5-3-b-sharedWorkerTweet .html
87 lines (81 loc) · 2.71 KB
/
Example-5-3-b-sharedWorkerTweet .html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Shared Web Workers: Twitter Example</title>
<meta name="author" content="Ido Green">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
#result {
background: orange;
padding: 20px;
border-radius: 18px;
}
#tweets {
background: grey;
border-radius: 28px;
padding: 20px;
}
</style>
</head>
<body>
<h1>Shared Web Workers: inner iframe</h1>
<nav>
<button id="start-button">Start The Shared Worker</button>
<button id="stop-button">Stop The Shared Worker</button>
</nav>
<article>
Inner iframe that in the real world could be another tab/window of our web app.
<div id="result"></div>
<div id="tweets"></div>
</article>
<script>
var worker;
function startWorker() {
console.log("WebWorker: Starting");
worker = new SharedWorker("Example-5-4-sharedWorkerTweet.js");
worker.port.addEventListener("message", function(e) {
var curTime = new Date();
// here we will show the messages between our page and the shared Worker
$('#result').append( curTime + " ) " + e.data + "<br/>");
var source = e.data[0].source;
// in case we have some data from Twitter - let's show it to the user
if (typeof source != 'undefined' ) {
$("#tweets").append("<ul>");
for (var i=0; i < 10; i++) {
$("#tweets").append("<li>" + e.data[i].text + " (" +
e.data[i].created_at + ")</li>");
}
$("#tweets").append("</ul>");
}
}, false);
worker.onerror = function(e){
throw new Error(e.message + " (" + e.filename + ":" + e.lineno + ")");
};
worker.port.start();
// post a message to the shared Web Worker
console.log("Calling the worker with @greenido as user");
worker.port.postMessage({
cmd: "start",
user: "greenido"});
}
function stopWorker() {
if (worker != undefined) {
worker.port.postMessage({ cmd: "stop" });
console.log("WebWorker: Stop the party");
// You might use worker = null if you wish not to use the worker from now
}
}
// when the DOM is ready - attached our 2 actions to the buttons
$(function() {
$('#start-button').click(function() {
startWorker();
});
$('#stop-button').click(function() {
stopWorker();
});
});
</script>
</body>
</html>