Can someone help me with time function PLEASE?

Line 335 https://codepen.io/TuscannyCodes/pen/WNpogJW?editors=0110

Hello Im working on this chatbot project and can’t for the life of me figure out how to put this time response inside a setTimeout so that the time will automatically update. I can manage to get the time to display, but I cannot get the formatted time inside of the setTimeout or SetInterval.

I know the code may be a little complicated because of how I played it all out and there is alot to look at but I left comments and the section that Im talking about is on line 335. Please help me figure out what im doing wrong.

let updatedTime = setInterval(currentTime,1000);
mainParagraph.textContent = `The time is ` + updatedTime;

updatedTime is not being set to the current time. I would read the MDN docs to see what it is actually being set to.

The setInterval method is working correctly though, it is calling currentTime() every second. This function is where you will want to update the time display on the page.

Cute project, here’s an example of how your timer would display the current time, based on your code with minor changes:

  if (inputBox.value.includes(" time")) {
    let timerId = setInterval(currentTime, 1000);

    counter++;
    inputBox.value = "";

    function currentTime() {
      let updatedTime = new Date().toLocaleString("en-US", {
        hour: "numeric",
        minute: "numeric",
        second: "numeric",
        hour12: true
      });

      mainParagraph.textContent = `The time is ` + updatedTime;
    }
  }

Not sure if it does what you expect. Have fun playing with it.

1 Like

Ok great that works! Although @bbsmooth Im still having problems understanding what was the issue in the first place. I can see from the MDN docs that it had something to do with the return value of the setInterval :thinking: but I can’t really understand what was going on… I wish that I could get a better understanding as to why @jsdisco 's syntax works and my original one didn’t. Possibly because all of the code needs to be defined inside of the If code block :face_with_monocle: ?

now I need to work on how to set up a clearInterval because it won’t leave the Interval function :sob:

It’s not my syntax, it’s my logic that’s different. If you want to update a timer every second, your setInterval callback needs to do some updating stuff. My code is generating a new Date every second, while your original code had a time variable defined once when your script started, but that variable’s value never changed. It doesn’t magically update and always holds the actual current time.

As for your clearInterval - you can see in my code that there’s a variable timerId that is equal to the return value of setInterval. Log it out to see what it is. You can use that to clear the interval with clearInterval(timerId).

1 Like

ok I get that now. Yeah this part is really confusing for me.

I did that and created another if block for the clearInterval with an id of 1, but its not working. im still working on it…

if( mainParagraph.textContent.includes(The time is ) && inputBox.value == keywords ){

clearInterval(1)

}

so my logic is:

IF the response is reading the time, (only accessible from the last function) AND if that time is currently displayed but the user enters another keyword, THEN clear the interval with the id of 1. :face_with_monocle:

But its not working.

You can’t just do this. When you initially call setInterval it returns a reference to the timer that you can use to kill it.

let timerId = setInterval(currentTime, 10);

timerId holds the reference and that’s what you will pass into clearInterval to kill the timer. It’s up to you to make sure that timerId is in scope wherever you need to use it.

1 Like

if( mainParagraph.textContent.includes(The time is ) && inputBox.value == keywords ){

clearInterval(timerId)

}

I don’t understand why this is not working.

furthermore, im not sure how I will be able to get out of the time response I have set up here and get back to my other responses if the user types in other keywords… I dont understand how to get out of this if block.

Ahh I see now. I got it working. I see how this works now, just took me a while. Thanks for the help guys :+1:

I totally get that now looking back on it :sweat_smile: sometimes it takes me a while to step away from it to see the bigger picture. Staring at code can hypnotize us in a way (at least for me it does :man_shrugging:) .

Thanks so much for your help you really helped me understand.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.