Welcome back.
How's your game coming along in this lesson we're going to be adding in an event listener to the element
that we're creating dynamically so using that same div object that we've already created in the earlier
lesson, we're going to add a click event to that and we can do that function as well as an anonymous
function contained within this block of code.
So we want to run this block of code.
So let's get our end time.
We already tracked our start time.
So that was set when the element was created and the end time is going to be set whenever the element
gets clicked.
So it's going to give us two values to string values.
And when we do date now what that produces, this produces a Unix timestamp in milliseconds from I believe
it's January 1st, 1970, and on the number of milliseconds till now.
So it's always going to be different.
So if I do it again, I see that's already incremented a little bit.
There's almost a full second of incrementing here and it's going to continuously increment.
So again, two milliseconds.
So it's always going to be a different value or almost always, unless you're typing really super fast.
So next, we need to be able to check to see what the differences between the end time and start time.
So let's create a variable for I can just call it dif.
We'll take our end time variable, subtracting the div start time value.
So that's the one that we set whenever the element was created and we're going to divide it by a thousand
so we get a value within seconds.
Now we can output it into the score.
So we've got an element here within score.
So taking that score object, we can add our inner HTML to that.
And then here's where we've got our value.
Second, and we also want the ability to restart the game, of course.
So where we've hit that start button, let's saw that start button by setting its style properties to
block.
And we also want to remove from the container.
So taking that container object, we're going to remove a child.
And the child that we're moving is that div that we created.
So I think we're ready to try that out.
So save try it out.
Wait till the element shows up and our score is not defined because we defined it a score area.
So let's make that update and try that one more time.
And that's how many seconds it was clicked in.
So something went wrong with our math here or we've got to get around that before we divide it by a
thousand.
So try that one more time.
Sometimes it does take a little bit of playing through just to make sure that it's functioning as needed.
So we clicked it in one point zero two seconds and this time we got one point six three five seconds.
So go ahead and add the click functionality into your game and you can be ready to move on to the next
step.
We're going to do some improvements.