When a button is pressed, an animation loop is triggered. At the start of the loop, I create a new time object.
I also have a pause button. A problem was created when paused and then started as the Date
object would be recreated each time.
The variable is global and not defined to be anything. I could set it to 0 and run the conditional test on that instead, but either way there has to be a cleaner way of doing this. Apologies, I'm quite new to JS.
The code works, but it's ugly (in my opinion):
var t;//not defined
//for starting the animation
startSim.onclick = function(){
if(t == undefined){
//get time object
t = new Date();
}
//other code...
}
EDIT: In case the question is unclear - I'm checking to see if the Date object has been created by finding out if the variable is undefined or not. What I'm asking is simply Is there a way to find out if the Date object already exists that doesn't rely on checking if the var is undefined. There is little code as the above is the only relevant part, I can post the rest but it's pointless as not required. (I've added a little more to show structure but I don't think it's required to understand the question)
t = t || new Date()
which either keepst
if it is defined or assigns thenew Date()
value \$\endgroup\$