1

I have been trying to get a function to reload every second.

I can do this with php get file contents (and meta page reload) but I am also using JS to display time and its not instant at loading so it has a flashing effect.

I have tried this with JS but it is showing nothing.

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vivarium Enviroment Control Centre</title>
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
    function updateTime() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        if (seconds < 10){
            seconds = "0" + seconds;
        }
        var v = hours + ":" + minutes + ":" + seconds + " ";
        if(hours > 11){
            v+="PM";
        } else {
            v+="AM"
        }
        setTimeout("updateTime()",1000);
        document.getElementById('time').innerHTML=v;
    }
updateTime();

$("document").ready(function(){
    setInterval(function(){
        $("#wifi").load('wifiout.php');
    },1000);
});


function changeStatus() {
    var image = document.getElementById('lightStatus');
    if (image.src.match("lightoff")) {
        image.src = "lighton.png";
    } else {
        image.src = "lightoff.png";
    }
}
</script>
</head>
<body>
<div id="topbar">
    <span id="time"></span>
    <div id="wifi"></div>
    <img id="lightStatus" class="right" onclick="changeStatus()" src="lightoff.png" width="32" height="32">
</div>
</body>
</html>
2
  • You are using jQuery syntax but I don't see it included? Commented Apr 3, 2016 at 21:45
  • This is something I've never done before so it is very possible. Commented Apr 3, 2016 at 21:46

1 Answer 1

1

Working fiddle

You're missing jQuery plugin declaration, add the following line before your script tag :

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>

NOTE : call the updateTime() function inside ready function or you will get the following error :

TypeError: Cannot set property 'innerHTML' of null

Because you're trying to use span time when the page not fully loaded yet.

Hope this will helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Q updated to reflect change made. The file wifiout.php only contains an image tag, and its not being displayed on the page.
Working example here acharki.byethost31.com/test.html, check my update the problem was in updateTime() call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.