Note that there are some explanatory texts on larger screens.

plurals
  1. POInitializing a repetitive action with Javascript with getTime()
    primarykey
    data
    text
    <p>I'm a newb to development, so this may come off as a stupid question, but I figured I'd ask anyway. After all, me looking bad just makes you look better. :)</p> <p>I want to change the css style on an element based on time. I've tried a few different methods and can get the time to display inside of html, but I can't use the time to trigger other events. I've put this little page together to make things simpler for me.</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;title&gt;timerTest&lt;/title&gt; &lt;style type="text/css"&gt; #box { height:200px; width: 200px; background-color: red; } &lt;/style&gt; &lt;/head&gt; &lt;body onload="maFucktion()"&gt; &lt;div id="box"&gt;T&lt;/div&gt; &lt;script type="text/javascript"&gt; var box = document.getElementById('box'); function maFucktion() { var d = new Date(); for(i=0; i &gt; 100; ++i) { if((d.getTime() % 1000) &lt; 499) { box.style.backgroundColor = "blue"; box.innerHTML = d.getTime() % 1000; } else { box.style.backgroundColor = "red"; box.innerHTML = d.getTime() % 1000; } } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>So, what my little brain tells me this should do is, on page load, execute maFucktion() which should initiate a for loop which:</p> <p>(1)sets a new Date() (2)gets the time since january 1 1970 in milliseconds with the getTime() method (3)breaks it down to the half second with the modulus operator (4)and delivers a new background color and the division remainder of the condition based on whether the value is between 0-499 or else</p> <p>I want it to change box.style.backgroundColor every half second which should end up looking like one of those silly banner ads from 1998, but I can't get it to automatically change. </p> <p>I know a for loop probably isn't the best, but it should at least display a new innerHTML value for #box, right? </p>
    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.
    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