Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 73cb4b1

Browse files
committed
add storage object to provide utilities for localstore
1 parent e24e7b8 commit 73cb4b1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

runestone/common/js/bookfuncs.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,3 +1137,34 @@ function injectCodeCoach(div_id) {
11371137
'div_id': div_id
11381138
});
11391139
}
1140+
1141+
var storage = {
1142+
set: function(directive, value) {
1143+
localStorage.setItem(directive+"_value", value);
1144+
localStorage.setItem(directive+"_timestamp", $.now());
1145+
},
1146+
remove: function(directive) {
1147+
localStorage.removeItem(directive+"_value");
1148+
localStorage.removeItem(directive+"_timestamp");
1149+
},
1150+
get: function(directive) {
1151+
return localStorage.getItem(directive+"_value");
1152+
},
1153+
has: function(directive) {
1154+
return localStorage.getItem(directive+"_value") !== null;
1155+
},
1156+
// Tests whether the server has the newer version
1157+
is_new: function(directive, server_time) {
1158+
var stored_time = localStorage.getItem(directive+"_timestamp");
1159+
return (server_time >= stored_time+5000);
1160+
},
1161+
};
1162+
1163+
var timers = {};
1164+
function addDelay(directive, action, delay) {
1165+
if (delay === undefined) {
1166+
delay = 400;
1167+
}
1168+
clearTimeout(timers[directive]);
1169+
timers[directive] = setTimeout(action, delay);
1170+
}

0 commit comments

Comments
 (0)