-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.whileinuse.js
More file actions
53 lines (45 loc) · 1.54 KB
/
jquery.whileinuse.js
File metadata and controls
53 lines (45 loc) · 1.54 KB
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
/*!
* While In Use jQuery utility v0.0.1
*
* Copyright 2014 Rob Widmer
* Released under the MIT license
* http://github.com/rdubya/whileinuse
*/
(function($, window){
'use strict';
var internalSetTimeout = window.setTimeout, // Save a few bytes when minified
$window = $(window); // Avoid wrapping the window object every time
function listenOnce(func) {
$window.one('mousemove touchstart', func);
}
$.whileInUse = function(action, interval) {
var active = true; // Default this to true so it will always run the first time
// Only create this function once so it is reused when the handler to register activity fires
function markActive() {
active = true;
}
// Only create this function once so it is reused when the handler for switching from inactive to active fires
function executeAsync() {
// asynchronously execute with a slight delay so a small mouse movement
// only runs the action once and doesn't count as still being active
// for the next interval
internalSetTimeout(runner, 500);
}
function runner() {
if (active) {
active = false;
// If the action returns false we will stop the interval
// returning nothing or true will continue the interval
if (action() != false) {
internalSetTimeout(runner, interval);
listenOnce(markActive);
}
} else {
// No events have fired since the last execution
// Set up a handler to restart the intervals when action happens
listenOnce(executeAsync);
}
}
internalSetTimeout(runner, interval);
};
})(jQuery, window);