I've got a bit of javascript that hides a button if a set of criteria aren't yet met. (it hides the "NEXT" button if the customer hasn't added all required services to their basket)
I need the javascript to fire every time the customer reaches the basket and understand this can be done via an event listener.
To reach the basket the customer will always click a button which has a classname of "bookly-hour"
Is it possible to make the javascript fire a few seconds after the click of the "bookly-hour" button?
Edit / Update
Q:
"Can you show us the code that you got for now Do you have all the code that the button should trigger, but not the button click event handler?" ...
A:
That's right I have the code the button needs to trigger. I need help with triggering it when the button is clicked. The code:
const sessions = ['session 1', 'session 2', 'session 3']; const searchTarget = document.querySelector('.bookly-cart-step'); const submitButton = document.querySelector('.bookly-next-step'); if (searchTarget.textContent.includes('session')) { if ( !sessions.every(session => searchTarget.textContent.includes(session) ) ) { submitButton.setAttribute('disabled', ''); } else { submitButton.removeAttribute('disabled'); } };
You can use the window.setTimeout function, inside of your onClick
handler
EDIT:
To add the click event handler you could do
submitButton.addEventListener('click', yourHandler);
Where yourHandler
is the function you want to trigger
If you want the code inside of yourHandler to fire few seconds later you could do
submitButton.addEventListener('click', () => window.setTimeout(yourHandler, 5000)); // 5s later
(or put the setTimeout inside the handler function)