1$(window).on("blur focus", function(e) {
2 var prevType = $(this).data("prevType");
3
4 if (prevType != e.type) { // reduce double fire issues
5 switch (e.type) {
6 case "blur":
7 // do work
8 break;
9 case "focus":
10 // do work
11 break;
12 }
13 }
14
15 $(this).data("prevType", e.type);
16})
1var interval_id;
2$(window).focus(function() {
3 if (!interval_id)
4 interval_id = setInterval(hard_work, 1000);
5});
6
7$(window).blur(function() {
8 clearInterval(interval_id);
9 interval_id = 0;
10});