source: gs3-extensions/web-audio/trunk/js-mad/sink.js-master/src/core/do-interval.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 1.2 KB
Line 
1void function (Sink) {
2
3/**
4 * Creates a timer with consistent (ie. not clamped) intervals even in background tabs.
5 * Uses inline workers to achieve this. If not available, will revert to regular timers.
6 *
7 * @static Sink
8 * @name doInterval
9 *
10 * @arg {Function} callback The callback to trigger on timer hit.
11 * @arg {Number} timeout The interval between timer hits.
12 *
13 * @return {Function} A function to cancel the timer.
14*/
15
16Sink.doInterval = function (callback, timeout) {
17 var timer, kill;
18
19 function create (noWorker) {
20 if (Sink.inlineWorker.working && !noWorker) {
21 timer = Sink.inlineWorker('setInterval(function (){ postMessage("tic"); }, ' + timeout + ');');
22 timer.onmessage = function (){
23 callback();
24 };
25 kill = function () {
26 timer.terminate();
27 };
28 } else {
29 timer = setInterval(callback, timeout);
30 kill = function (){
31 clearInterval(timer);
32 };
33 }
34 }
35
36 if (Sink.inlineWorker.ready) {
37 create();
38 } else {
39 Sink.inlineWorker.on('ready', function () {
40 create();
41 });
42 }
43
44 return function () {
45 if (!kill) {
46 if (!Sink.inlineWorker.ready) {
47 Sink.inlineWorker.on('ready', function () {
48 if (kill) kill();
49 });
50 }
51 } else {
52 kill();
53 }
54 };
55};
56
57}(this.Sink);
Note: See TracBrowser for help on using the repository browser.