1window.addEventListener('load', function()
2{
3 var xhr = null;
4
5 getXmlHttpRequestObject = function()
6 {
7 if(!xhr)
8 {
9 // Create a new XMLHttpRequest object
10 xhr = new XMLHttpRequest();
11 }
12 return xhr;
13 };
14
15 updateLiveData = function()
16 {
17 var now = new Date();
18 // Date string is appended as a query with live data
19 // for not to use the cached version
20 var url = 'livefeed.txt?' + now.getTime();
21 xhr = getXmlHttpRequestObject();
22 xhr.onreadystatechange = evenHandler;
23 // asynchronous requests
24 xhr.open("GET", url, true);
25 // Send the request over the network
26 xhr.send(null);
27 };
28
29 updateLiveData();
30
31 function evenHandler()
32 {
33 // Check response is ready or not
34 if(xhr.readyState == 4 && xhr.status == 200)
35 {
36 dataDiv = document.getElementById('liveData');
37 // Set current data text
38 dataDiv.innerHTML = xhr.responseText;
39 // Update the live data every 1 sec
40 setTimeout(updateLiveData(), 1000);
41 }
42 }
43});
44
1function data() {
2 var ret = [];
3 ret.push({
4 y: 'Today',
5 a: <?php echo $data; ?>
6 });
7 return ret;
8 }
9
10var graph = Morris.Bar({
11 element: 'graph',
12 data: data(),
13 xkey: 'y',
14 ykeys: ['a'],
15 labels: ['random label']
16 });
17
18function update() {
19 graph.setData(data());
20 }
21
22setInterval(update, 60000);
23