I'm working on an application that will allow me to click a button on the web page and then start my timer object. However, the timer will not start onclick.
<script type="text/javascript">
var Timer = function()
{
// Property: Frequency of elapse event of the timer in millisecond
this.Interval = 1000;
// Property: Whether the timer is enable or not
this.Enable = new Boolean(false);
// Event: Timer tick
this.Tick;
// 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);
};
};
//counts down the timer
function timer_tick()
{
index = index - 1;
document.getElementById("blue").innerHTML =index;
if (index === 0)
{
obj.Stop();
}
}
If I remove function startBlue() but leave the code inside of it, the timer will run on page load. So after looking at my code I think the issue is where I call startBlue (below in the html) or somewhere in this function:
function startBlue(){
var index = 300;
var obj = new Timer();
obj.Interval = 1000;
obj.Tick = timer_tick();
obj.Start();
}
</script>
<body>
<div id ="Objectives">
Your Objectives:
<br>
<br>
<label>Blue Buff:</label>
<button id="yosb" onclick ="startBlue();">Start Blue</button>
<h2 id = "blue">
</h2>
</div>
</html>