function sendMessage(){
...
if(file){
sendingMessage = true;
setTimeout(() => {
sendingMessage = false;
messages = [...messages, chatmessage];
}, 3800)
}
chatmessage = '';
inputRef.focus()
updateScroll();
}
Right now when this function is called, chatmessage
is first set to an empty string, etc. then the code in the timeout function is executed which is the corect behaviour. However, is there a way make setTimeout synchronous?
This is the behaviour I want:
- If
if(file)
is true, setsendingMessage = true
(sendingMessage triggers a spinning wheel animation) - Wait for 3.8 seconds and execute the content in the timeout function.
- Only then, set
chatmessage
to empty string, etc (rest of the code)
Edit 1:
I can just move the code inside the timeout function but the issue is in my case, if file
is not present, it should just skip the timeout function alltogether. Then I would have to write another if block to check if the file is not present to execute the same commands (duplicate code). Another way to solve this would be to put the same code in a function and call it both places but feel like its not the ideal way to go and creating a function to change the value of three variables seems like overkill.