Commit 48c8b070fcd47a26f874f34e2474731dcda374f4
1 parent
16fdf54d
changed ajax to get json and integrate a javascript countdown.
Showing
2 changed files
with
40 additions
and
14 deletions
@@ -45,7 +45,6 @@ | @@ -45,7 +45,6 @@ | ||
45 | "left: 200px;" \ | 45 | "left: 200px;" \ |
46 | "top: 100px;" \ | 46 | "top: 100px;" \ |
47 | "position: fixed;" \ | 47 | "position: fixed;" \ |
48 | - "overflow: none;" \ | ||
49 | "background-color: white;" \ | 48 | "background-color: white;" \ |
50 | "border: 1px solid black;" \ | 49 | "border: 1px solid black;" \ |
51 | "}" \ | 50 | "}" \ |
@@ -59,23 +58,53 @@ | @@ -59,23 +58,53 @@ | ||
59 | "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \ | 58 | "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \ |
60 | "<script>" \ | 59 | "<script>" \ |
61 | "$(document).ready(function() {" \ | 60 | "$(document).ready(function() {" \ |
61 | + "var intervalId;" \ | ||
62 | + "var vnext = 0;" \ | ||
63 | + "var counter = function() {" \ | ||
64 | + "if (0 >= vnext) {" \ | ||
65 | + "$.getJSON(\"/randval/\", function(data) {" \ | ||
66 | + "var date = new Date(data.ctime * 1000);" \ | ||
67 | + "$(\"#ctime\").empty().append(date.toString());" \ | ||
68 | + "vnext = data.vnext;" \ | ||
69 | + "$(\"#value\").empty().append(data.value);" \ | ||
70 | + "});" \ | ||
71 | + "} else {" \ | ||
72 | + "vnext--;" \ | ||
73 | + "}" \ | ||
74 | + "$(\"#vnext\").empty().append(vnext);" \ | ||
75 | + "};" \ | ||
76 | + "$(\"#msg\").ajaxError(function(event, request, settings) {" \ | ||
77 | + "$(this).append(" \ | ||
78 | + "\"<li>Error requesting page \" + " \ | ||
79 | + "settings.url + " \ | ||
80 | + "\"</li>\");" \ | ||
81 | + "});" \ | ||
62 | "$(\"a\").click(function() {" \ | 82 | "$(\"a\").click(function() {" \ |
63 | - "$(\"#randval\").load(\"/randval/\");" \ | 83 | + "intervalId = setInterval(counter, 1000);" \ |
64 | "$(\"#randval\").removeClass(\"hide\");" \ | 84 | "$(\"#randval\").removeClass(\"hide\");" \ |
65 | "});" \ | 85 | "});" \ |
66 | "$(\"#randval\").click(function() {" \ | 86 | "$(\"#randval\").click(function() {" \ |
87 | + "clearInterval(intervalId);" \ | ||
88 | + "vnext = 0;" \ | ||
67 | "$(\"#randval\").addClass(\"hide\");" \ | 89 | "$(\"#randval\").addClass(\"hide\");" \ |
68 | "});" \ | 90 | "});" \ |
69 | "});" \ | 91 | "});" \ |
70 | "</script>" \ | 92 | "</script>" \ |
71 | "</head>" \ | 93 | "</head>" \ |
72 | "<body>" \ | 94 | "<body>" \ |
73 | - "<div id=\"randval\" class=\"hide\"></div>" \ | 95 | + "<div id=\"randval\" class=\"hide\">" \ |
96 | + "<span class=\"small\">" \ | ||
97 | + "Value created at: <br /><span id=\"ctime\"></span><br>" \ | ||
98 | + "Next value in: <span id=\"vnext\"></span><br />" \ | ||
99 | + "</span>" \ | ||
100 | + "Value: <span id=\"value\"></span>" \ | ||
101 | + "</div>" \ | ||
74 | "<div id=\"main\">" \ | 102 | "<div id=\"main\">" \ |
75 | "<h1>Testpage</h1>" \ | 103 | "<h1>Testpage</h1>" \ |
76 | "<img src=\"/image/\" />" \ | 104 | "<img src=\"/image/\" />" \ |
77 | - "<hr /><a href=\"#\">Link</a>" \ | 105 | + "<br /><a href=\"#\">Link</a>" \ |
78 | "</div>" \ | 106 | "</div>" \ |
107 | + "<hr /><div id=\"msg\"></div>" \ | ||
79 | "</body>" \ | 108 | "</body>" \ |
80 | "</html>" | 109 | "</html>" |
81 | 110 |
@@ -33,9 +33,10 @@ | @@ -33,9 +33,10 @@ | ||
33 | #include "http/message.h" | 33 | #include "http/message.h" |
34 | #include "http/header.h" | 34 | #include "http/header.h" |
35 | 35 | ||
36 | -#define RESP_DATA "<span class=\"small\">" \ | ||
37 | - "Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \ | ||
38 | - "<br />Value: %02d" | 36 | +//#define RESP_DATA "<span class=\"small\">" \ |
37 | +// "Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \ | ||
38 | +// "<br />Value: %02d" | ||
39 | +#define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":%02d}" | ||
39 | 40 | ||
40 | HttpResponse | 41 | HttpResponse |
41 | httpResponseRandval(time_t ctime, int value) | 42 | httpResponseRandval(time_t ctime, int value) |
@@ -45,7 +46,6 @@ httpResponseRandval(time_t ctime, int value) | @@ -45,7 +46,6 @@ httpResponseRandval(time_t ctime, int value) | ||
45 | HttpResponse response; | 46 | HttpResponse response; |
46 | HttpMessage message; | 47 | HttpMessage message; |
47 | size_t nbuf; | 48 | size_t nbuf; |
48 | - struct tm * tmp; | ||
49 | time_t remaining; | 49 | time_t remaining; |
50 | 50 | ||
51 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); | 51 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
@@ -55,17 +55,14 @@ httpResponseRandval(time_t ctime, int value) | @@ -55,17 +55,14 @@ httpResponseRandval(time_t ctime, int value) | ||
55 | new(HttpHeader, | 55 | new(HttpHeader, |
56 | "Content-Type", | 56 | "Content-Type", |
57 | sizeof("Content-Type")-1, | 57 | sizeof("Content-Type")-1, |
58 | - "text/html", | ||
59 | - sizeof("text/html")-1)); | 58 | + "application/json", |
59 | + sizeof("application/json")-1)); | ||
60 | 60 | ||
61 | message->type = HTTP_MESSAGE_BUFFERED; | 61 | message->type = HTTP_MESSAGE_BUFFERED; |
62 | 62 | ||
63 | - tmp = localtime(&ctime); | ||
64 | - nbuf = strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %T %Z", tmp); | ||
65 | - | ||
66 | remaining = 30 - (time(NULL) - ctime); | 63 | remaining = 30 - (time(NULL) - ctime); |
67 | 64 | ||
68 | - nbuf = sprintf(buffer, RESP_DATA, timebuf, remaining, value); | 65 | + nbuf = sprintf(buffer, RESP_DATA, ctime, remaining, value); |
69 | 66 | ||
70 | message->nbody = nbuf; | 67 | message->nbody = nbuf; |
71 | message->body = malloc(nbuf); | 68 | message->body = malloc(nbuf); |
Please
register
or
login
to post a comment