Below is my code. When I'm calling myDate
variable outside of myTime()
function to create a Date()
object, the setInterval()
function don't fire but when it's inside myTime()
function. As I know myDate
variable is a global variable and it should work same inside or outside of the function. But why here the setInterval()
method not firing while I'm creating the Date()
object outside of the function? Experts explain the things. TIA
var myDate = new Date();
function myTime(){
document.getElementById('text').innerHTML = myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
}
setInterval(myTime, 1000);