How to delay going to a link after clicking?

Hi all! I’m brand new to this but enjoying learning the ropes.

I’m designing a page where I’d like to create the following experience:

  1. you click on a linked element
  2. there’s a brief pause while some animations on the current page play out (namely, the other options fade out)
  3. you’re then directed to the new page (in the same window) that the element you clicked on links to.

I’ve tried using setTimeout to do this, but with no luck so far. I tried using it with the .link function but all that did was make the link not clickable for the first 3 seconds, haha. ;9 So it seems that function sets what it links to but doesn’t mean “go to link”. What’s the function I want here?

I had read elsewhere that if you want to add this kind of delay to all links on your page (which I do) you can add the following code at the top:

import wixWindow from ‘wix-window’;

function delay (URL) {
setTimeout( function () {wixWindow.location = URL }, 2000 );
}

But that didn’t work for me – the links still open up immediately.

Thanks so much!! :slight_smile:

Hi 2 Bunnies,

You probably need to do a couple of things here.

When you look at the show() and hide() functions in the documentation: HiddenMixin - Velo API Reference - Wix.com

You will see that the functions return something called a Promise.

Promises are documented technically here: Promise - JavaScript | MDN
There is also a primer article on Wix use of Promises here: Velo: Working with Promises | Help Center | Wix.com

In a nutshell think of a Promise as a form of tracking code that you might get from Amazon when you order something on line. Your order doesn’t arrive immediately it takes time. So you get a tracking identifier which gives you progress information but more importantly for Amazon and you it tells you when the item was delivered or something else bad happened.

This is what a Promise is used for. When something that takes time and doesn’t happen immediately (for example talking to a server over the internet) is executed you get a Promise as a result. The Promise may return a value OR nothing. In many cases, if you don’t depend on the result of these functions you can simply execute them and they will finish at some point without you needing to worry.

If you want to do something with the result or, in your case KNOW, that the action you requested has finished or failed then you need to use the promise. Remember, like the tracking code, the Promise will only let the code you need to execute to run when what was ‘promised’ has completed (been delivered). This is where the two key Promise functions .then() and .catch() can be useful.

In your case you want to wait for the elements, that you want to be hidden, to hide themselves before you exit the page. One way you could do this is to chain the hide() calls like this:

// Fade the first element
$w('#element1').hide()
.then (() => {
    // First element has disappeared
    // Hide element 2
    // By returning the promise from hide we make the code easier to 
    // Read by pushing the .then processing to the outer promise (element1).
    return $w'(#element2').hide();
})
.then(() => {
   // Second element has disappeared
    // Hide element 3
   return $w'(#element3').hide();
})
.then(() => {
    // All elements have been hidden Do something else
    // For example you could add a delay timer here also but that would need to be in a Promise to work
    // Effectively e.g.
    let keepMyPromise = new Promise((resolve, reject) => {
        setTimeout(resolve, 2000, '2 second timer expired');
    });
    return keepMyPromise;
})
.then((timerMessage) => {
    console.log('Element fade out completed redirect to new page['+timerMessage+']');
    wixLocation.to('/<nextPage>');
})
.catch((error) => {
    console.log('An Error occurred');
    console.log(error);
});

WARNING: Since I do not know what your page looks like please consider the above code to be an example of what your code COULD look like. Since I have no idea what elements are on your page or how you redirect to the linked page then there may be other things you need to get this to work.

Hope you find this useful

Steve

1 Like

Wow, thank you so much, Steve! It was incredibly generous of you to write all of that up for me, and I really appreciate it.

I tried integrating this code (adapting it for my objects, etc.) and it didn’t work – it gave an error for the “let” line, and the other effects didn’t work. However, this code did everything I wanted!

import wixLocation from ‘wix-location’;

export function hoverBox2_click(event, $w) {
//Add your code for this event here:
$w(‘#hoverBox1’).hide(“fade”,500);
$w(‘#hoverBox3’).hide(“fade”,500);
$w(‘#hoverBox4’).hide(“fade”,500);
setTimeout( function () {wixLocation.to(“/stressed”),2500});
}

You answered my question in that the function I wanted was “wixLocation.to()”, not “.link”. Thank you so much!!! :smiley:

You are welcome. Glad I could help.

1 Like

Hey, I am working on a similar issue. I want to inform my users via a popup notice that they’ll be forwarded to an external site when they click on an external link:

  1. Click on external link

  2. Popup (e.g. lightbox) opens

  3. redirect to external website (address of external link)

The code above is not working in my case. Any ideas?

Please add your own issue into a new post instead of bumping up an old post. Explain what you are trying to do, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines.

This post is an old post and is being closed.