2013-07-08 76 views
0

你好,我有一個通過PHP執行定時器/ HTML這樣如何使用jquery問題重新啓動javascript函數

// 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); 
} 
+0

什麼是'controlRedirTimer'? – Blender

+0

@Blender它是一個功能的嘗試,我刪除它,因爲它不相關了,抱歉的混亂 – Squirtle

回答

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(); 
} 
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>