Line 0
Link Here
|
0 |
- |
1 |
/* |
|
|
2 |
Copyright (c) 2007, Andreas Blixt, http://blixt.org/. All rights reserved. |
3 |
Code licensed under the MIT License: |
4 |
http://www.opensource.org/licenses/mit-license.php |
5 |
*/ |
6 |
|
7 |
// waitTime is in milliseconds. |
8 |
var ScreenSaver = function (waitTime) { |
9 |
this.lastActivity = new Date().getTime(); |
10 |
this.waitTime = waitTime; |
11 |
|
12 |
var $this = this; |
13 |
this._timer = setInterval(function () { $this._checkTime.call($this) }, 1000); |
14 |
document.onmousemove = function () { $this._mouseHandler.call($this) }; |
15 |
}; |
16 |
|
17 |
ScreenSaver.prototype = { |
18 |
_timer: null, |
19 |
|
20 |
lastActivity: 0, |
21 |
started: false, |
22 |
waitTime: 0, |
23 |
|
24 |
onstart: function () {}, |
25 |
onend: function () {}, |
26 |
|
27 |
dispose: function () { |
28 |
if (this._timer) clearInterval(this._timer); |
29 |
document.onmousemove = null; |
30 |
}, |
31 |
|
32 |
_checkTime: function () { |
33 |
if (!this.started && new Date().getTime() - this.lastActivity >= this.waitTime) { |
34 |
this.started = true; |
35 |
this.onstart(); |
36 |
} |
37 |
}, |
38 |
|
39 |
_mouseHandler: function () { |
40 |
this.lastActivity = new Date().getTime(); |
41 |
if (this.started) { |
42 |
this.started = false; |
43 |
this.onend(); |
44 |
} |
45 |
} |
46 |
}; |
47 |
|
48 |
/*********** Begin Example *********** |
49 |
|
50 |
var ss = new ScreenSaver(5000); |
51 |
|
52 |
ss.onstart = function () { |
53 |
document.getElementsByTagName("body")[0].style.backgroundColor = "#000"; |
54 |
}; |
55 |
|
56 |
ss.onend = function () { |
57 |
document.getElementsByTagName("body")[0].style.backgroundColor = "#fff"; |
58 |
}; |
59 |
|
60 |
************* End Example *************/ |