-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautosave.js
More file actions
65 lines (40 loc) · 884 Bytes
/
autosave.js
File metadata and controls
65 lines (40 loc) · 884 Bytes
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
var AutoSave = (function(){
var timer = null;
function getEditor(){
var elems = document.getElementsByTagName("textarea")
if (elems.length <= 0)
return null;
return elems[0];
}
function save(){
var editor = getEditor();
if (editor) {
localStorage.setItem("AUTOSAVE_" + document.location, editor.value )
}
}
function restore(){
var saved = localStorage.getItem("AUTOSAVE_" + document.location)
var editor = getEditor();
if (saved && editor){
editor.value = saved;
}
}
return {
start: function(){
var editor = getEditor();
if (editor.value.length <= 0)
restore();
if (timer != null){
clearInterval(timer);
timer = null;
}
timer = setInterval(save, 5000);
},
stop: function(){
if (timer){
clearInterval(timer);
timer = null;
}
}
}
}())