how to add a setTimeout on an onclick [duplicate] - javascript

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Calling functions with setTimeout()
(6 answers)
SetTimeout not delaying a function call [duplicate]
(7 answers)
Why is setTimeout executing immediately? [duplicate]
(4 answers)
Why is my function call that should be scheduled by setTimeout executed immediately? [duplicate]
(3 answers)
Closed 5 months ago.
I have an add to cart button. When I click on it two things happen at the same time. The class b-items__item__add-to-cart and the onclick.
I would like the onclick to be able to be executed 2 seconds after pressing the add to cart button
With my code it does not work
<a style="cursor: pointer; margin-bottom: 5px;" data-nom="2001" data-prix="1.10" data-qte="1" data-checkbox="2001" data-url="https://phil.pecheperle.be/image-perles/perle-verre-peche-gardon-2001.JPG" class="btn btn-primary ajouter-panier b-items__item__add-to-cart" onclick="setTimeout(ouvreMaJolieAlert(event), 2000);">add to cart</a>

You're attempting to give the output of ouvreMaJolieAlert(event) to setTimeout as the callback. The function executes, but the output value of that function is probably nothing, or something else (depends on your specific scenario), meaning that the delayed invocation fails. You've probably meant to create a wrapper over it:
setTimeout(() => ouvreMaJolieAlert(event), 2000)
However, personally, I'd recommend you to create a function that handles this from a script; or better yet, use the addEventListener method:
document.querySelector("#the-id-of-your-link-here").addEventListener("click", () => {
setTimeout(() => ouvreMaJolieAlert(event), 2000));
});
And if you're planning to create multiple links that will perform this action, you can just replace the ID with a class.

Related

How can i correctly pass a function to a eventListener? [duplicate]

This question already has answers here:
Javascript, addEventListener callback function executes immediately and only once? [duplicate]
(3 answers)
Closed 1 year ago.
I made a basic eventListener which creates an alert on button click. This one doesn't work (it calls the alert on the page load)
function handleClick(){
alert("hi!");
}
document.querySelector("button").addEventListener("click",handleClick());
this one works instead, but i don't understand why
document.querySelector("button").addEventListener("click",function(){
alert("hello");
});
with handleClick() you are executing the function when that statement is read. You need to pass in the function handleClick reference.
This:
document.querySelector("button").addEventListener("click",handleClick);

Why console.log() bypasses addEventListener? [duplicate]

This question already has answers here:
addEventListener calls the function without me even asking it to
(5 answers)
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 1 year ago.
I have a button which on click is supposed to execute the addEventListener function. On the callback function part I directly use a console.log.
<button id="btnTrans">Translate</button>
var buttonTranslate = document.querySelector("#btnTrans");
buttonTranslate.addEventListener("click", console.log("clicked"));
as soon as the DOM loads, "clicked" appears on the console. Shouldn't it wait for the event to happen first?
The console.log is immediately executed because it is not wrapped in a function. Wrap it in a function:
buttonTranslate.addEventListener("click", () => { console.log("clicked") });

EventListener calling a function() prematurely [duplicate]

This question already has answers here:
Javascript "addEventListener" Event Fires on Page Load [duplicate]
(2 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
My EventListener is calling the function before the click with this code... what's wrong with that line?
It was working fine when I had the function inside the parameter, but now that i'm calling it from there instead it's calling it prematurely before any click occurs.
divs[i].addEventListener('click', xopush(i), null);
It's calling a function with and argument passing to the function()... not an alert.
//Instead of EventListener I usually prefer to use...
divs[i].onclick = function() {
xopush(i);
}
... which is a answer to this. However, I was looking forward to using the eventListener so I could turn it off after 1 click on the element.

setInterval JS function doesn't work? [duplicate]

This question already has answers here:
Why is my function call that should be scheduled by setTimeout executed immediately? [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to call a function every x-seconds,
setInterval( alert("Hello"),2000);
But the alert function appear just for the first call,
You may try this:
setInterval(function() {
alert("Hello");
} ,2000);
It's not working like you thought. The alert fires/runs directly without interval because you've directly called it. To be more clerer, in your code:
setInterval(alert("Hello"),2000);
You are calling the alert("Hello") but you should pass a function (or bind) which you want to run on an interval, which is:
setInterval(function(){
// ...
}, 2000);
So the passed anonymous function will be called using the given interval (bind is another way but I've used a function/closure for simplicity to clarify you).

Javascript setTimeout at onclick [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am trying to call a javascript function at the onclick of an html element as shown below.
But not sure why the below timeout function is not working here.Any idea
onclick="setTimeout(rating(), 3000)"
remove parenthesis, change:
onclick="setTimeout(rating(), 3000)"
to
onclick="setTimeout(rating, 3000)"
or better way would be:
onclick = setTimeout(function() {
rating();
}, 3000);
onclick="setTimeout(rating, 3000)" // rating without the ()
When you include the parentheses, the function return value is used instead of the function itself.

Categories