Creating a timed redirect page with hidden API call and dynamic URL

Hey everyone, I’m new to web development and could use some help. I want to make a page that does three things:

  1. Show a countdown timer for 10 seconds when a user lands on it
  2. Make a hidden API call during those 10 seconds (to trigger a Zapier workflow)
  3. Redirect the user to a new URL after the countdown, where part of the URL comes from the API call

I can handle basic HTML, but I’m lost when it comes to the JavaScript part. How can I put this all together? Is there a simple way to do this without fancy frameworks?

Thanks for any tips or code examples you can share!

Having worked on a similar project, I can offer some insights. You’ll want to use JavaScript’s setTimeout() function for the countdown and redirection. For the API call, the Fetch API is your best bet. Here’s a basic approach:

Create a div in your HTML to display the countdown. Use setTimeout() to decrement the counter every second and update the div. Within this function, make your API call using fetch(). Store the response in a variable.

When the countdown reaches zero, construct your new URL using the API data and redirect with window.location.href. Remember to handle potential API failures gracefully.

One tip: Consider adding a loading indicator during the countdown to improve user experience. Also, test thoroughly across different browsers to ensure consistent behavior. Good luck with your project!

I’ve tackled something similar recently, so I’ll share what worked for me. You can accomplish this with vanilla JavaScript and the Fetch API. Here’s a rough outline:

Set up your HTML with a div for the countdown. Use setInterval() to create the countdown timer. Within the same function, use fetch() to make your API call and store the response in a variable. Once the countdown hits zero, use window.location.href to redirect, incorporating the API data into the URL.

The trickiest part was handling the API response timing, so I ended up using async/await to ensure I had the data before redirecting. Also, be sure to use clearInterval() to stop the timer when it’s done. One gotcha: some browsers block redirects that aren’t triggered by user interaction, so you might need to add a ‘Continue’ button as a fallback. Hope this helps point you in the right direction!

hey there! i’ve done something like this before. here’s a quick tip:

use setInterval for the countdown and fetch for the API call. store the API response in a variable. when the timer hits 0, use window.location.href to redirect with the API data in the URL.

watch out for browser redirect blocks tho. maybe add a button as backup. good luck!