user3288788
user3288788

Reputation: 45

Simple JS Refresh Div Not Working

I've found a simple JS script that should refresh a div every 1000 milliseconds however it is not working, any help is appreciated.

Here's the script:

window.setInterval("refreshDiv()", 1000);  
function refreshDiv(){  
    document.getElementById("timer").innerHTML;
}

Here's the div:

<div id="timer">
    <div class="progress-bar" style="width:
    <?php
        echo (Pot::getTime(1) * 100) / 120;                                 
    ?>%">
    </div>
</div>

The script is within a file called script.js and is loaded at the bottom of the page like so:

<script src="/styles/js/script.js"></script>

Thank you in advance for any responses :-)

Upvotes: 0

Views: 184

Answers (2)

Dallin
Dallin

Reputation: 1074

From the code you've provided it looks like you're not doing anything with the div when refreshDiv is called. If you are trying to change the text with innerHTML it needs to be something like document.getElementById("timer").innerHTML = 'something'

This javascript should work:

window.setInterval(refreshDiv, 1000);  
function refreshDiv(){  
    document.getElementById("timer").innerHTML = 'example';
}

Note: using no quotes with just the function name in setInterval like refreshDiv instead of "refreshDiv()" is the recommended way.

Upvotes: 1

javinor
javinor

Reputation: 674

Remove the quotes:

window.setInterval(refreshDiv, 1000);  

setInterval expects to get a function, not a string.

Upvotes: 0

Related Questions