JavaScript getTimeout?
-
21-09-2019 - |
質問
の ウインドウです。setTimeout (関setInterval機のJavascriptするための機能を提供します。機能が実行されるいずれかの時点での未来:
id = setTimeout(function, delay);
が"遅れ"のミリ秒数入では、いつでもどこでもたいていの機能呼び出されます。この時間を過ぎると、キャンセルすることができるタイマーを用い:
clearTimeout(id);
だいたい は 更新 タイマー.こういうことができるように進または遅延タイマーの機能を得られるようになれば、ミリ秒 早く または 降 より、当初予定しています。
があったとしたらgetTimeout方法、なにができるようなもの:
originally_scheduled_time = getTimeout(id);
updateTimeout(id, originally_schedule_time + new_delay); // change the time
したことがいえなくていいよgetTimeoutまたはその更新、既存のタイマー.
があるので、アクセスのリスト予定アラームおよび修正ですか。
がよりよいアプローチを考えていますか。
よろしく!
解決
またこのような機能を使って記入する必要があります。
このラッパーのためのsetTimeout話が返すオブジェクトを使用できる"延期"のタイマ:
function setAdvancedTimer(f, delay) {
var obj = {
firetime: delay + (+new Date()), // the extra + turns the date into an int
called: false,
canceled: false,
callback: f
};
// this function will set obj.called, and then call the function whenever
// the timeout eventually fires.
var callfunc = function() { obj.called = true; f(); };
// calling .extend(1000) will add 1000ms to the time and reset the timeout.
// also, calling .extend(-1000) will remove 1000ms, setting timer to 0ms if needed
obj.extend = function(ms) {
// break early if it already fired
if (obj.called || obj.canceled) return false;
// clear old timer, calculate new timer
clearTimeout(obj.timeout);
obj.firetime += ms;
var newDelay = obj.firetime - new Date(); // figure out new ms
if (newDelay < 0) newDelay = 0;
obj.timeout = setTimeout(callfunc, newDelay);
return obj;
};
// Cancel the timer...
obj.cancel = function() {
obj.canceled = true;
clearTimeout(obj.timeout);
};
// call the initial timer...
obj.timeout = setTimeout(callfunc, delay);
// return our object with the helper functions....
return obj;
}
var d = +new Date();
var timer = setAdvancedTimer(function() { alert('test'+ (+new Date() - d)); }, 1000);
timer.extend(1000);
// should alert about 2000ms later
他のヒント
と思います。よりよいアプローチが書ご自身のラッパーを店舗におト(func-ref、遅延、およびタイムスタンプ).ることができるように更新、タイマーによる清算し、計算コピーした。
他のラッパー:
function SpecialTimeout(fn, ms) {
this.ms = ms;
this.fn = fn;
this.timer = null;
this.init();
}
SpecialTimeout.prototype.init = function() {
this.cancel();
this.timer = setTimeout(this.fn, this.ms);
return this;
};
SpecialTimeout.prototype.change = function(ms) {
this.ms += ms;
this.init();
return this;
};
SpecialTimeout.prototype.cancel = function() {
if ( this.timer !== null ) {
clearTimeout(this.timer);
this.timer = null;
}
return this;
};
使用量:
var myTimer = new SpecialTimeout(function(){/*...*/}, 10000);
myTimer.change(-5000); // Retard by five seconds
myTimer.change(5000); // Extend by five seconds
myTimer.cancel(); // Cancel
myTimer.init(); // Restart
myTimer.change(1000).init(); // Chain!
できないんだけとにかく、も利用でき、サービスをご紹介します。.
あ溶液によって同僚にバカにした言動を繰り返すことで特別なハンドラ機能の停止、開始タイムアウトが必要です。でも広く使われている場合を作成する必要があります少しの遅延のための推移です。のようにしたいときに隠mouseoverメニューのないこの時期に、マウス葉ですが、数ミリ秒です。それ以外の場合はマウスが、取り消しが必要にタイムアウト.
ですから呼び出される関数を getDelayedHandlers.たとえば、機能を持っているところを示し、メニューを隠す
function handleMenu(show) {
if (show) {
// This part shows the menu
} else {
// This part hides the menu
}
}
きを遅ハンドラによってそう:
var handlers = handleMenu.getDelayedHandlers({
in: 200, // 'in' timeout
out: 300, // 'out' timeout
});
handlers
となるオブジェクトがハンドラの機能が呼び解除、その他のタイムアウト.
var element = $('menu_element');
element.observe('mouseover', handlers.over);
element.observe('mouseout', handlers.out);
P.S.のための本ソリューションの仕事に必要な拡張機能のクライアントに、ネットワーク curry
機能は、自動的に行われ 試作.
ができるようになります:
if (this condition true)
{
setTimeout(function, 5000);
}
elseif (this condition true)
{
setTimeout(function, 10000);
}
else
{
setTimeout(function, 1000);
}
とってもおかだの構築の条件や論理です。感謝