user2282228
user2282228

Reputation: 1

Make clock running

I would like the time to reset every second so the clock becomes a running one. I'm a javascript noob and I couldn't find any solution anywhere.

        <!--
        var currentDate = new Date()
        var day = currentDate.getDate()
        var month = currentDate.getMonth() + 1
        var year = currentDate.getFullYear()
        document.write("<b>" + day + "/" + month + "/" + year + "</b>")
        //-->
    <!--
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


document.write(hours + ":" + minutes + ":" + seconds)

Upvotes: 0

Views: 98

Answers (2)

holographic-principle
holographic-principle

Reputation: 19738

var myInterval = window.setInterval(function() {
  window.document.write(hours + ":" + minutes + ":" + seconds);
}, 1000);

Later you can stop it with

window.clearInterval(myInterval);

We assign the return value of setInterval (an ID in the form of a number) to a variable because we'll need it later to stop our particular interval using the clearInterval function. If we don't do that, there will be no way (without certain hacks) to stop the interval.

Upvotes: 1

Erik Schierboom
Erik Schierboom

Reputation: 16636

For that you need to use the window.setInterval method. Please look at this page for more information: http://www.w3schools.com/js/js_timing.asp

Upvotes: 0

Related Questions