2012-05-26 35 views
1

我有這樣的足球計數腳本計數腳本

// Class: Timer 
var Timer = function (callback) { 
    // Property: Frequency of elapse event of the timer in milliseconds 
    this.Interval = 1000; 

    // Property: Whether the timer is enable or not 
    this.Enable = new Boolean(false); 

    // Event: Timer tick 
    this.Tick = callback; 

    // Member variable: Hold interval id of the timer 
    var timerId = 0; 

    // Member variable: Hold instance of this class 
    var thisObject; 

    // Function: Start the timer 
    this.Start = function() { 
     this.Enable = new Boolean(true); 

     thisObject = this; 
     if (thisObject.Enable) { 
      thisObject.timerId = setInterval(
      function() { 
       thisObject.Tick(); 
      }, thisObject.Interval); 
     } 
    }; 

    // Function: Stops the timer 
    this.Stop = function() { 
     thisObject.Enable = new Boolean(false); 
     clearInterval(thisObject.timerId); 
    }; 

}; 

// Namespace: Match rules and timings 
var Match = { 

    Timers: { 
     FirstHalf: new Timer(TimerTick), 
     HalfTime: new Timer(TimerTick), 
     SecondHalf: new Timer(TimerTick), 
     TickCount: -1 
    }, 

    Strings: { 
     FirstHalf: 'First Half', 
     HalfTime: 'Half Time', 
     SecondHalf: 'Second Half', 
     FullTime: 'Finished' 
    }, 

    DisplayTime: function (t) { 
     var m = parseInt(t/60); 
     var s = t % 60; 
     return (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s); 
    } 
}; 

// Function: Tick Event Handler (callback function) 
function TimerTick(timer) { 

    // Document elements used. 
    var TimerP = document.getElementById('time'); 
    var DisplayP = document.getElementById('display'); 

    // During First Half 
    if (Match.Timers.FirstHalf.Enable == true) { 
     if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 } 
     if (Match.Timers.TickCount == 2700) { 
      Match.Timers.FirstHalf.Stop(); 
      Match.Timers.TickCount = -1; 
      Match.Timers.HalfTime.Start(); 
     } else { 
      TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount); 
      DisplayP.innerHTML = Match.Strings.FirstHalf; 
      Match.Timers.TickCount++; 
     } 
    } 

    // During Half Time 
    else if (Match.Timers.HalfTime.Enable == true) { 
     if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 } 
     if (Match.Timers.TickCount == 900) { 
      Match.Timers.HalfTime.Stop(); 
      Match.Timers.TickCount = -1; 
      Match.Timers.SecondHalf.Start(); 
     } else { 
      TimerP.innerHTML = '45:00'; 
      DisplayP.innerHTML = Match.Strings.HalfTime + ' (' + Match.DisplayTime(900 - Match.Timers.TickCount) + ')'; 
      Match.Timers.TickCount++; 
     } 
    } 

    // During Second Half 
    else if (Match.Timers.SecondHalf.Enable == true) { 
     if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 2700 } 
     if (Match.Timers.TickCount == 5400) { 
      TimerP.innerHTML = '90:00'; 
      DisplayP.innerHTML = Match.Strings.FullTime; 
      Match.Timers.SecondHalf.Stop(); 
      Match.Timers.TickCount = -1; 
     } else { 
      TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount); 
      DisplayP.innerHTML = Match.Strings.SecondHalf; 
      Match.Timers.TickCount++; 
     } 
    } 
} 

function KickOff() { 
    var btn = document.getElementById('btnKickOff'); 
    btn.setAttribute('style','display: none;'); 
    Match.Timers.FirstHalf.Start(); 
} 

或點擊此處查看http://pastebin.com/CkmPQ9ZV

和我對劇本此HTML代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
     <title>Simple Football Match Timer</title> 
     <script src="timer.js" type="text/javascript"></script> 
    </head> 

    <body> 
     <form id="pageForm" runat="server"> 
      <div>  
       <p id="display">Waiting for kick off.</p> 
       <p id="time">00:00</p> 
       <input id="btnKickOff" type="button" value="Kick Off!" onclick="KickOff();" /> 
      </div> 
     </form> 
    </body> 
</html> 

這個腳本開始計數當我點擊踢開..它從0到45分鐘後計數,它顯示半時間和倒計時15分鐘後休息15分鐘完成後,它再次開始從45到90再次計數,當它達到90它顯示「已完成」

其良好的腳本,但我的問題是,我希望這個腳本不要在每次頁面刷新後再次啓動,我想將它發佈在我的網站上,所以當用戶打開我的網站時,他們將能夠看到什麼比賽的時間是..我會點擊它的比賽開始..並且它繼續,直到結束

PS:我不擅長Javascript ..我得到了創建此腳本的幫助:)

+3

'new Boolean(false);'。我沒有見過這麼長的時間... – elclanrs

回答

0

你可以設置一個特定的unix時間來啓動它,然後讓它引用它而不是「timerId」變量。

2

如果您希望每個人在訪問您的網站時都看到相同的內容,即相同的匹配時間,則不能像這樣使用JavaScript。 JavaScript在用戶的計算機上運行,​​所以當你啓動計時器時,只有你會看到它。

這很難做到,除非您在服務器上存儲一個JavaScript可以訪問的開始時間,以便找出它的匹配程度。就我個人而言,我會用MySQL創建一個數據庫表,並將所有匹配和它們的開始時間存儲在其中。然後,您可以使用PHP訪問它,然後調用httprequest將其轉換爲JavaScript。然而,這樣做可能有更簡單的方法。

+0

啊哈... thx的信息:) – Meh