Commit 48c8b070fcd47a26f874f34e2474731dcda374f4

Authored by Georg Hopp
1 parent 16fdf54d

changed ajax to get json and integrate a javascript countdown.

... ... @@ -45,7 +45,6 @@
45 45 "left: 200px;" \
46 46 "top: 100px;" \
47 47 "position: fixed;" \
48   - "overflow: none;" \
49 48 "background-color: white;" \
50 49 "border: 1px solid black;" \
51 50 "}" \
... ... @@ -59,23 +58,53 @@
59 58 "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \
60 59 "<script>" \
61 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 82 "$(\"a\").click(function() {" \
63   - "$(\"#randval\").load(\"/randval/\");" \
  83 + "intervalId = setInterval(counter, 1000);" \
64 84 "$(\"#randval\").removeClass(\"hide\");" \
65 85 "});" \
66 86 "$(\"#randval\").click(function() {" \
  87 + "clearInterval(intervalId);" \
  88 + "vnext = 0;" \
67 89 "$(\"#randval\").addClass(\"hide\");" \
68 90 "});" \
69 91 "});" \
70 92 "</script>" \
71 93 "</head>" \
72 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 102 "<div id=\"main\">" \
75 103 "<h1>Testpage</h1>" \
76 104 "<img src=\"/image/\" />" \
77   - "<hr /><a href=\"#\">Link</a>" \
  105 + "<br /><a href=\"#\">Link</a>" \
78 106 "</div>" \
  107 + "<hr /><div id=\"msg\"></div>" \
79 108 "</body>" \
80 109 "</html>"
81 110
... ...
... ... @@ -33,9 +33,10 @@
33 33 #include "http/message.h"
34 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 41 HttpResponse
41 42 httpResponseRandval(time_t ctime, int value)
... ... @@ -45,7 +46,6 @@ httpResponseRandval(time_t ctime, int value)
45 46 HttpResponse response;
46 47 HttpMessage message;
47 48 size_t nbuf;
48   - struct tm * tmp;
49 49 time_t remaining;
50 50
51 51 response = new(HttpResponse, "HTTP/1.1", 200, "OK");
... ... @@ -55,17 +55,14 @@ httpResponseRandval(time_t ctime, int value)
55 55 new(HttpHeader,
56 56 "Content-Type",
57 57 sizeof("Content-Type")-1,
58   - "text/html",
59   - sizeof("text/html")-1));
  58 + "application/json",
  59 + sizeof("application/json")-1));
60 60
61 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 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 67 message->nbody = nbuf;
71 68 message->body = malloc(nbuf);
... ...
Please register or login to post a comment