Share feedback, ideas and get community help

Updated last week

setTimeout Script Help

At a glance
The community member wanted to give a user 60 seconds to make a payment in their Typebot flow, and was experimenting with the Script block and setTimeout JS Window method. They encountered an issue where the Script block to cancel the refresh didn't work, even though it worked in the Chrome Dev Console. Another community member provided a solution: instead of using a Script block to define the timerId JS variable, they used a Set variable block to store the setTimeout as a Typebot variable. Then, in a subsequent Script block after the button, they used clearTimeout({{timerId}}) to clear the saved timeout. This solution worked for the original poster.
Useful resources
Use Case: I want to give a user 60 seconds to make a payment when they get to the Stripe block in my Typebot flow.

I couldn't find a clean solution to this, so I was experimenting with the Script block and setTimeout JS Window method.

Here's the problem I encountered: I'm trying to start a "timer" with setTimeout to refresh the page using the Script Block and then cancel the refresh in a subsequent Script block after the user clicks on a button. However, when I run the Script block to cancel the refresh, it doesn't do it. It works fine when I run this same exercise in the Chrome Dev Console.

Please see my screen recording: https://neferlopez.dubb.com/v/5NZnE2

Any suggestions?
Marked as solution
Heya nefer!
After watching your recording, reproducing your current flow, thinking of and implementing the solution I had in mind, I can tell you I found a fix to your issue (and why yours was happening)!

Let me walk you through it:

Initial Issue
  • For some reason, even when defining your cancelRefresh function in the same script block as the setTimeout, cancelRefresh seems to not "exist" in the client like it does in the local console (the Typebot error stating that the function doesn't exist confirms my guess)
Solution
  • Instead of a Script Block to define your timerId JS variable that holds the setTimeout, I created a Set variable block that sets a new Typebot variable timeId, with the following code:
Plain Text
setTimeout(function() {
  window.location.reload(true);
}, 3000);


We are now sure it would store that Timeout as a Typebot variable for later use.

  • Next, after your button, use a Script Block (like you currently have), and put the following code in it:
Plain Text
clearTimeout({{timerId}})


This would clear the saved timeout.

I tested and it works. Hopefully it works for you too!

Here's a screenshot of the final flow:
View full solution
A
n
B
7 comments
Heya nefer!
After watching your recording, reproducing your current flow, thinking of and implementing the solution I had in mind, I can tell you I found a fix to your issue (and why yours was happening)!

Let me walk you through it:

Initial Issue
  • For some reason, even when defining your cancelRefresh function in the same script block as the setTimeout, cancelRefresh seems to not "exist" in the client like it does in the local console (the Typebot error stating that the function doesn't exist confirms my guess)
Solution
  • Instead of a Script Block to define your timerId JS variable that holds the setTimeout, I created a Set variable block that sets a new Typebot variable timeId, with the following code:
Plain Text
setTimeout(function() {
  window.location.reload(true);
}, 3000);


We are now sure it would store that Timeout as a Typebot variable for later use.

  • Next, after your button, use a Script Block (like you currently have), and put the following code in it:
Plain Text
clearTimeout({{timerId}})


This would clear the saved timeout.

I tested and it works. Hopefully it works for you too!

Here's a screenshot of the final flow:
Attachment
image.png
Excellent response! I sincerely appreciate your thorough explanation, it worked wonderfully πŸ˜€

One little observation I noticed was I toggled off the "Execute on client?" option by accident.

It needs to be turned on, which makes sense because the user's window client is what I'm trying to refresh.

Thank you very much, @Anthony πŸ™πŸ»
Attachment
Screenshot_2025-01-08_at_8.59.29_AM.png
Awesome!
I thought I wouldn't mention the Execute on client? option as I saw it checked for both of your Script blocks, but I'm glad you figured it out. It indeed needs to be enabled to access the window object.

You're welcome, I'm happy it worked! πŸ˜€
That's why I love wandering around support places because we always discover new use cases we would have never thought of ourself haha

Feel free to mark your own post here as "βœ… Solved" for help posts filtering if you feel like it ✌️
Good call!

🌟 Solved by @Anthony
The way we "Mark a post as Solved" here is by adding Tags on a per-post basis, which allows to sort and filter per tag, where "βœ… Solved" is one of the tags

You'll find a step-by-step tutorial on how to do that in my attached image:
Attachments
Help_and_Questions_Tags.png
Marking_a_post_as_Solved.png
Really cool, I didn't know I could mark as solved πŸ˜…
Clever πŸ‘
Add a reply
Sign up and join the conversation on Discord