So i want my page to reload an amount of seconds after the button is clicked. Right now it reloads every 5 seconds automatically. But if i put the timeout function in the addEvenlistener then it errors. What can i do to solve it?
button.addEventListener(
'click',
() =>
(cover.style.visibility = 'hidden') && (button.style.visibility = 'hidden'),
);
setTimeout(function () {
window.location.reload();
}, 5000);
Weird, this should work. Are you sure you didn't forget the curly braces in the click callback?
button.addEventListener('click', () => {
cover.style.visibility = 'hidden';
button.style.visibility = 'hidden';
setTimeout(function () {
window.location.reload();
}, 5000);
});
You just need to write it inside the EventListener as @Jonas mentioned. And also you can have that timeout value to be dynamic as well
var timeoutValue = 5000
button.addEventListener('click', () => {
cover.style.visibility = 'hidden';
button.style.visibility = 'hidden';
setTimeout(function () {
window.location.reload();
}, timeoutValue);
});