0

こんにちは、このようにphp/htmlを介して実行するタイマーがあります

// timer
print "<script>countdown();</script>";

ただし、このようなjquery警告ダイアログがあります。閉じるボタンをクリックした後にjavascript関数を再起動したいと思います

    // Create timeout warning dialog
    $('body').append('<div title="Timeout"</div>');
    $('#sessionTimeout-dialog').dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        closeOnEscape: false,
        open: function(event, ui) { $(".dialog").hide(); },
        buttons: {
            // Button two - closes dialog and makes call to keep-alive URL
            "Continue Session": function() {
                $(this).dialog('close');
                clearTime();
                $.ajax({
                    type: 'POST',
                    url: o.keepAliveUrl
                });


                countdown(); //I call the javascript function in hopes of making it go again
            }
        }
    });

ただし、ボタンをクリックして続行すると、タイマーを実際に再起動できないようです。ここに私のタイマーコードがあります、私は何を間違っていますか? ボタンが閉じられた後にJavaScript関数を呼び出しますが、何も起こらず、負の数になります

      // set minutes
var mins = 2;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;   

function countdown() {
    setTimeout('Decrement()',1000);
}

function Decrement() {
    if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
    // if less than a minute remaining
    if (seconds < 59) {
        seconds.value = secs;
    } else {
        minutes.value = getminutes();
        seconds.value = getseconds();
    }
        secs--;
        setTimeout('Decrement()',1000);
    }
}

function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;
}

function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
}
4

2 に答える 2

0

カウントダウン関数はタイマーをリセットせず、デクリメント ループを開始するだけのようです。

のようなものを追加

function resetCountdown()
{
    secs = mins * 2;
}

ダイアログが閉じられたときにこの関数を呼び出して、カウントダウンをリセットします。countdown() を再度呼び出さないでください。これにより、既に実行されているタイマーに加えて別のタイマーが作成されます。

また、コードにエラーがあります:

if (seconds < 59) {
    seconds.value = secs;
} else {
    minutes.value = getminutes();
    seconds.value = getseconds();
}

する必要があります

if (secs < 59) {
    minutes.value = 0;
    seconds.value = secs;
} else {
    minutes.value = getminutes();
    seconds.value = getseconds();
}
于 2013-07-08T21:55:39.143 に答える
0

これがあなたが望むものかどうかは完全にはわかりませんが、

ウィンドウが(ダイアログから)フォーカスを取り戻したときに、特定のタイマーを再起動するのが好きです。以下に例を示します。

<html>
    <head>
        <script>
            g = {};
            g.timer = false;
            g.count = false;
            g.waittime = 3; //seconds for timer.
            g.timeleft = (g.waittime * 100) - 10;
            checktimer = function(){
                if(g.timer === false){
                    g.timer = setTimeout(function(){
                        g.timer = false;
                        g.timeleft = g.waittime * 100;
                        alert("Hi!");
                    },g.waittime * 1000);
                }
                if(g.count === false){
                    g.count = setInterval(function(){
                        g.timeleft-=10;
                        document.getElementById("disp").innerText =(g.timeleft/100).toString().match(/^\d+(?:\.\d{0,2})?/);
                    },100);
                }
            }
            window.onfocus = function() {
                checktimer();
            };

        </script>
    </head>
    <body onload="checktimer();">
        <div>Time Left: <span id="disp">0</span> Seconds.</div>
    </body>
</html>
于 2013-07-08T22:39:54.280 に答える