I’m trying to write to the console events together with the current time and date, but the time does not change with events. How can I update the variable of time every second?
var cde = function(){
var dte=new Date(),
mce=dte.getMonth()+1,
day=dte.getDate(),
hrs=dte.getHours(),
mns=dte.getMinutes(),
sec=dte.getSeconds();
if(mce<10){mce='0'+mce}
if(day<10){day='0'+day}
if(hrs<10){hrs='0'+hrs}
if(mns<10){mns='0'+mns}
if(sec<10){sec='0'+sec}
return
'['+day+'.'+mce+'.'+dte.getFullYear()+'::'+hrs+':'+mns+':'+sec+']';
}
Create a new date every time you make a log.
When you make a date in JS…
… no matter how many times you call
date, it’s still going to be equal to the exact millisecond it was created (based on the user’s system-clock).Wrap what you’ve got there in a function, instead of an immediately-invoked function, and call it.