Trigger button delay

Hi! I want to delay the time between clicking on the button and triggering its action (in this case, redirecting to a subpage). How can I do this?

Thanks in advance.

use setTimeout method (inside the button onClick() function):
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

And how can I translate this so it works with Corvid? I’m new into this, could you write the code for me? Thanks.

@b4443569 , put the following inside $w.onReady() function:

$w("#button1").onClick((event) => {
setTimeout(() => {/*Your Action Code;*/}, 1000) //time in milliseconds
})

@jonatandor35 Thank you for explaining it. But if I put it inside $w.onReady() it appears a message that says:
Parsing error: “import” and “export” may only appear at the top level.
Also, what do you mean with:

/*Your Action Code;*/

@b4443569 You’re doing something wrong. You probably don’t use the code I posted above but a combination of your ode and mine.
If you post your code here, I’ll be able to tell you where the problem is.

As for: /Your Action Code;/ replace it by your code. I don’t know what you’d like to happen on button click. But whatever you want, put it there.

@jonatandor35
I’m using this:

export function gallery1_itemClicked(event) {
  setTimeout(() => {/*Your Action Code;*/}, 1000)
}

The gallery is already configured to redirect to a different subpage depending the item you click on via Wix Editor. What am I doing wrong? Again, thanks for your help.

Also, I don’t know if I should create a different post for this but how do I hide a strip after a period of time (e.g 5 sec.) ?

@b4443569 OK. So if you use , export function for click detection, you have to put it outside the $w.onReady() function (in the code above I used an alternative method, that doesn’t include the export function).
If you want the click to redirect to another page, then do the following:

  1. On the editor: change to : when clicked nothing happens.
  2. at the top of your page code, add this line:
import wixLocation from 'wix-location';
  1. In the onItemClicked function write:
export function gallery1_itemClicked(event) {
  setTimeout(() => {wixLocation.to(event.item.link);}, 1000)
}

I had a typo. Fixed

I’ve made some changes. If it doesn’t work for you, let me know, and we’ll try something else.

@jonatandor35 I don’t understand the .link part. Could you clarify it, please?

@b4443569 ignore it. Use the code as is.

If it doesn’t work (and it might be problematic), then try the the following code (but first try the previous code):

 import wixLocation from 'wix-location';
$w.onReady(() => {
$w("#dataset1").onReady(() => {
$w("#dataset1").getItems(0, 1000)
.then((res) => {
let items = res.items;
$w("#gallery1").onItemClicked((event) => {
	setTimeout(() => {wixLocation.to(items[event.imageIndex]["url"])}, 1000);//instead of url, use the database field key; Instead of 1000, use the desired delay
})
})
});
})

(updated. You can keep it connected via the editor, Make sure it set (on the editor) to when clicked do nothing.)

@jonatandor35 I tried the first one and it worked! Thank you for your time.

@b4443569 You’re welcome. glad it worked :slight_smile: