Javascript setTimeout at onclick [duplicate] - javascript

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.

Related

JavaScript anonymous function syntax? [duplicate]

This question already has answers here:
Why do I have to omit parentheses when passing a function as an argument?
(2 answers)
JS: What's the difference between a ! closure and () closure? [duplicate]
(2 answers)
Closed 6 months ago.
I have a question. Why when I'm using anonymous function and I want to call it immediately i have to do like that:
(() => {console.log('something')})()
But when I want to use it with setTimeout for example, I don't have to use '(' at beginning and '()' at the end?
setTimeout(() => {console.log('something')}, 2000)
You pass the function as a parameter to setTimeout, rather than calling it right away. That's why you don't need the () at the end

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);

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).

setInterval and setTimeout don't seem to work with a console.log [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am answering my own question in the hopes it will solve someone else's headache:
I could not get the following code to work:
function givePosition(){
var place = $('#two').position();
console.log(place);
}
window.setInterval(givePosition(), 50);
The solution to my problem was to give the function name as a parameter without parentheses, like so:
window.setInterval(functionName, 50);
instead of:
window.setInterval(functionName(), 50)

Categories