SetTimeout and setInterval the first parameter plus no parentheses, plus no double quotes
Change it to :
setTimeout(numCount,1000);
With the ()
you are invoking the function immediately. Above is passing the function as a reference and it will be called after the delay time.
The other less preferred way using quotes is:
setTimeout('numCount()',1000); // or setTimeout("numCount()",1000);
Using string argument.. the string will be evaluated as script at end of timer delay
When in doubt look it up in the MDN docs
Javascript setTimeOut function confusion
The other two answers are correct, but just in case your question wasn't about anonymous functions but rather something like this
function onTimeout(){
console.log('foo');
};
setTimeout(onTimeout, 1000); // First form
setTimeout(onTimeout(), 1000); // Second Form
The difference is that in the first form, the function onTimeout is called after 1 seconds, which is typically the desired outcome.
In the second form, on Timout is called immediately, and whatever that function returned is called after 1 seconds; in this case undefined would be called because console.log returns undefined. So the function would execute immediately and nothing would happen after 1 second.
Function call using single quotes vs no quotes
Try doing thissetInterval(updateTime, 1000);
without ()
Now coming to the question why.
The Setinterval function evaluates the content if string and executes if function name
To explain why it does execute the updateTime() with brackets immediately is because it tries to execute the output of the updateTime function in interval loop, that will be undefined if you are not returning anything and will be treated as function name if it returns a string. Anything else will be overlooked or error thrown.
Hope that helps
What is the difference between func with parentheses and one without it
Because setState
is not guaranteed to be synchronous, it accepts a callback function as the second argument, which it will call after it has updated the component's state.
There is no guarantee of synchronous operation of calls to
setState
and calls may be batched for performance gains.
Passing the function as a reference (without parentheses) ensures that your onSubmit
method will only be called after the component's state has updated and you can rely on this.state
to contain the new value.
Calling the function yourself (with parentheses) means that your onSubmit
method will always execute before this this.state
has the correct value, because it will run before setState
does. If this is intentional behaviour it may make more sense to write it explicitly.
this.onSubmit()
this.setState({ number: number-1 });
Useless setTimeout call (missing quotes around argument?)
There already exists a jQuery-method delay and it expects a string(queueName) and not a function as parameter.
Choose another name for your delay-method to avoid conflicts.
return with parentheses and without gets different results
Putting anything on a new line in JS is a stupid idea, for this very reason. Automatic semi-colon insertion is killing your code, return;
is how the JS engine will interpret your code (which by itself is a valid statement). Put it like this:
function dateString2(date) {
return ('0' + date.getDate()).slice(-2) + '/' +
('0' + (date.getMonth()+1)).slice(-2) + '/' +
date.getFullYear();
}
Stay away from things like this:
if (something)
{
// logic
}
And always use this style:
if (something) {
// logic
}
SetTimeout() call without callback function behaves in unexpected manner
The number that you see in the console is the ID
returned by the setTimeout()
call, which you can use later to clear the time out i.e. cancel it.
For example:
const id = setTimeout(console.log, 1000, "this is cancelled");clearTimeout(id); //cancelling the earlier timeoutsetTimeout(console.log, 1000, "this is not cancelled");
How do I set a time out using JavaScript?
You can use the setTimeout
function that is built into JavaScript for this. It will not prevent the user from interacting with your page:
setTimeout(function()
{
yourFunctionHere();
},
13000);
Related Topics
How to Fire and Forget a Promise in Nodejs (Es7)
Why Does Google Prepend While(1); to Their JSON Responses
JavaScript % (Modulo) Gives a Negative Result for Negative Numbers
How to Submit a Form Using JavaScript
How to Replace Innerhtml of a Div Using Jquery
How to Resize Images Proportionally/Keeping the Aspect Ratio
How to Upload a File with the Js Fetch API
Window.Onbeforeunload and Window.Onunload Is Not Working in Firefox, Safari, Opera
How to Check a Not-Defined Variable in JavaScript
How to Gain Access to the Closure of a Function
What's Wrong with Awaiting a Promise Chain
How to Wait for Set of Asynchronous Callback Functions
JavaScript Setinterval and 'This' Solution
Parseint VS Unary Plus, When to Use Which
What's the Best Way to Convert a Number to a String in JavaScript
What's the Difference Between Window.Location and Document.Location in JavaScript
Are "(Function ( ) { } ) ( )" and "(Function ( ) { } ( ) )" Functionally Equal in JavaScript