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.
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 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 ?
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).
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.
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.
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.
I totally get that now looking back on it 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 ) .
Thanks so much for your help you really helped me understand.