はい、あなたはJavaScriptでこれを行うことができます。
function Counter(options) {
var timer;
var instance = this;
var seconds = options.seconds || 10;
var onUpdateStatus = options.onUpdateStatus || function() {};
var onCounterEnd = options.onCounterEnd || function() {};
var onCounterStart = options.onCounterStart || function() {};
function decrementCounter() {
onUpdateStatus(seconds);
if (seconds === 0) {
stopCounter();
onCounterEnd();
return;
}
seconds--;
};
function startCounter() {
onCounterStart();
clearInterval(timer);
timer = 0;
decrementCounter();
timer = setInterval(decrementCounter, 1000);
};
function stopCounter() {
clearInterval(timer);
};
return {
start : function() {
startCounter();
},
stop : function() {
stopCounter();
}
}
};
を...そして、それを使用する方法の例:ここでは、単純なカウンタの実装that was inspired from this StackOverflow answerだ私はしばらく前に見つけ
var countdown = new Counter({
// number of seconds to count down
seconds: 3,
onCounterStart: function() {
// show pop up with a message
...
},
// callback function for each second
onUpdateStatus: function(second) {
// change the UI that displays the seconds remaining in the timeout
...
},
// callback function for final action after countdown
onCounterEnd: function() {
// show message that session is over, perhaps redirect or log out
...
}
});
countdown.start();
サーバがユーザに警告する準備ができたら、ちょうど作成しますタイマーがカウントダウンを開始します。各イベントで何が起きるかをカスタマイズすることができます。タイマーが開始されたとき、秒が1秒ごとに、カウントダウンが完了したときにカスタマイズできます。
このスクリプトは、ユーザーがアプリケーション上の何かをクリックした場合、または銀行ウェブサイトのようなタイムアウトを自動的に検出した場合にのみ機能しますか? – alice7