-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
77 lines (64 loc) · 1.78 KB
/
test.js
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
/* Format time intervals for input to human brain */
function human_interval(span) {
if (span >= 86400*2)
return (span/86400).toFixed(2) + " days";
if (span >= 3600*2)
return (span/3600).toFixed(1) + " hours";
if (span >= 60*2)
return (span/60).toFixed(1) + " mins";
return Math.round(span) + " secs";
}
/* Debugging */
function log_init() {
$('#debug').html('<b>Debug:</b>');
}
function log_message(msg) {
e = $('#debug');
e.html(e.html() + '<br />\n' + msg);
}
/* AJAX */
/* Timeouts... */
var auto_refresh_s = 60;
var auto_refresh_divide = 2;
var auto_refresh_counter = 0;
var auto_refresher = setInterval(auto_refresh, auto_refresh_s * 1000 /
auto_refresh_divide);
var do_full_update = 0;
function auto_refresh() {
if (++auto_refresh_counter == auto_refresh_divide)
post_background_request({});
}
function toggle_enabled(code) {
ecode = $('#enabled_'+code).html();
if (ecode == 'enabled')
post_background_request({disable: code});
else if (ecode == 'disabled')
post_background_request({enable: code});
}
function post_background_request(params) {
rc = $('#refresh_indicator');
if (params.full_update) {
do_full_update = 1;
rc.html('Populating...');
} else
rc.html('Updating...');
/* Add some form inputs data. */
params.text_input1 = $('#example-text-input').val();
auto_refresh_counter = 0;
if (!($.post('cgi-bin/test.py', params,
update_states_from_server_data,
'json'))) {
rc.html('Update failed! (Try again)');
}
}
function update_states_from_server_data(data, status) {
// Callback for AJAX query.
d = $('#status_message');
d.html(data.date);
if ('also' in data)
$('#debug').html(data.also);
else
$('#debug').html('');
rc = $('#refresh_indicator');
rc.html('Refresh')
}