Created
April 11, 2012 03:28
-
-
Save mohayonao/2356651 to your computer and use it in GitHub Desktop.
別タブを選択しても精度の落ちないタイマー(Chrome, Firefox)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MutekiTimer = (function() { | |
var MutekiTimer = function() { | |
initialize.apply(this, arguments); | |
}, $this = MutekiTimer.prototype; | |
var TIMER_PATH = (function() { | |
var BlobBuilder, URL, builder; | |
BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder; | |
URL = window.URL || window.webkitURL; | |
if (BlobBuilder && URL) { | |
builder = new BlobBuilder(); | |
builder.append("var timerId = 0;"); | |
builder.append("this.onmessage = function(e) {"); | |
builder.append(" if (timerId !== 0) {"); | |
builder.append(" clearInterval(timerId);"); | |
builder.append(" timerId = 0;"); | |
builder.append(" }"); | |
builder.append(" if (e.data > 0) {"); | |
builder.append(" timerId = setInterval(function() {"); | |
builder.append(" postMessage(null);"); | |
builder.append(" }, e.data);"); | |
builder.append(" }"); | |
builder.append("};"); | |
return URL.createObjectURL(builder.getBlob()); | |
} | |
return null; | |
}()); | |
var initialize = function() { | |
if (TIMER_PATH) { | |
this._timer = new Worker(TIMER_PATH); | |
this.isMuteki = true; | |
} else { | |
this._timer = null; | |
this.isMuteki = false; | |
} | |
this._timerId = 0; | |
}; | |
$this.setInterval = function(func, interval) { | |
if (this._timer !== null) { | |
this._timer.onmessage = func; | |
this._timer.postMessage(interval); | |
} else { | |
if (this._timerId !== 0) { | |
clearInterval(this._timerId); | |
} | |
this._timerId = setInterval(func, interval); | |
} | |
}; | |
$this.clearInterval = function() { | |
if (this._timer !== null) { | |
this._timer.postMessage(0); | |
} else if (this._timerId !== 0) { | |
clearInterval(this._timerId); | |
this._timerId = 0; | |
} | |
}; | |
return MutekiTimer; | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function() { | |
var div1 = document.createElement("div"); | |
var div2 = document.createElement("div"); | |
document.body.appendChild(div1); | |
document.body.appendChild(div2); | |
var interval = 20; | |
var count1 = 0; | |
var count2 = 0; | |
var timer = new MutekiTimer(); | |
div1.style.color = timer.isMuteki ? "red" : "gray"; | |
timer.setInterval(function() { | |
div1.innerHTML = count1++; | |
}, interval); | |
window.setInterval(function() { | |
div2.innerHTML = count2++; | |
}, interval); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment