Difference Between Settimeout with and Without Quotes and Parentheses

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 this
setInterval(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



Leave a reply



Submit