Note that there are some explanatory texts on larger screens.

plurals
  1. POCant get onclick event to trigger my function in javascript
    primarykey
    data
    text
    <p>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. </p> <pre><code>&lt;script type="text/javascript"&gt; 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(); } } </code></pre> <p>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. </p> <pre><code>function startBlue(){ var index = 300; var obj = new Timer(); obj.Interval = 1000; obj.Tick = timer_tick(); obj.Start(); } &lt;/script&gt; &lt;body&gt; &lt;div id ="Objectives"&gt; Your Objectives: &lt;br&gt; &lt;br&gt; &lt;label&gt;Blue Buff:&lt;/label&gt; &lt;button id="yosb" onclick ="startBlue();"&gt;Start Blue&lt;/button&gt; &lt;h2 id = "blue"&gt; &lt;/h2&gt; &lt;/div&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload