setTimeout nuances in Node.js - javascript

I'm trying to understand how the callback function works inside the setTimeout function. I'm aware the format is: setTimeout(callback, delay) I wrote a little test script to explore this.
test1.js
console.log("Hello")
setTimeout(function () { console.log("Goodbye!") }, 5000)
console.log("Non-blocking")
This works as expected, printing Hello <CRLF> Non-blocking and then 5 seconds later, prints Goodbye!
I then wanted to bring the function outside of the setTimeout like this:
console.log("Hello")
setTimeout(goodbye(), 5000)
console.log("Non-blocking")
function goodbye () {
console.log("Goodbye")
}
but it doesn't work and there isn't a 5 second delay between Non-blocking and Goodbye!, they print straight after each other.
It works if I remove the brackets from the function call in the timeout, like this:
setTimeout(goodbye, 5000)
but this doesn't make sense to me because that's not how you call a function. Futhermore, how would you pass arguments to the function if it looked like this?!
var name = "Adam"
console.log("Hello")
setTimeout(goodbye(name), 5000)
console.log("Non-blocking")
function goodbye (name) {
console.log("Goodbye "+name)
}
My question is really, why doesn't it work when there are parameters in the function, despite the fact the setTimeout is being provided with a valid function with the correct syntax?

By putting the parentheses after your function name, you are effectively calling it, and not passing the function as a callback.
To provide parameters to the function you are calling:
You can pass an anon function. setTimeout(function(){goodbye(name)}, 5000);
Or, you can pass the arguments as a third parameter. setTimeout(goodbye, 5000, name);
Look at this question: How can I pass a parameter to a setTimeout() callback?

No matter where you place it, goodbye(name) executes the function immediately. So you should instead pass the function itself to setTimeout(): setTimeout(goodbye, 5000, name).

When you use it like this:
setTimeout(goodbye(), 5000);
it will first call goodbye to get its return value, then it will call setTimeout using the returned value.
You should call setTimeout with a reference to a callback function, i.e. only specifying the name of the function so that you get its reference instead of calling it:
setTimeout(goodbye, 5000);
To make a function reference when you want to send a parameter to the callback function, you can wrap it in a function expression:
setTimeout(function() { goodbye(name); }, 5000);
You can use parantheses in the call, but then the function should return a function reference to the actual callback function:
setTimeout(createCallback(), 5000);
function createCallback() {
return function() {
console.log("Goodbye");
};
}

Related

Javascript Callback functions vs just calling functions

So i don't really understand the point of "callback".
Here is an example of callback:
function sayBye(){
alert("Bye!");
}
function saySeeYou(){
alert("See you!");
}
function sayHello(name,myfunc){
alert("Hello");
myfunc;
}
sayHello("Max",saySeeYou());
Whats the point of passing in a function when you can just call the function? like this code does the exact same:
function sayBye(){
alert("Bye!");
}
function saySeeYou(){
alert("See you!");
}
function sayHello(name){
alert("Hello");
saySeeYou();
}
sayHello("Max");
Whats the point of passing in a function when you can just call the function?
Usually, callbacks Javascript are used in Javascript for code that you want to run in the future. The simplest example is setTimeout: if you call the callback now then the code runs immedieately instead of after 500 ms.
//prints with a delay
console.log("Hello");
setTimeout(function(){
console.log("Bye");
}, 500);
//no delay this time
console.log("Hello");
console.log("Bye");
Of course, it would be really neat if we could write something along the lines of
//fake Javascript:
console.log("Hello");
wait(500);
console.log("Bye");
But sadly Javascript doesnt let you do that. Javascript is strictly single-threaded so the only way to code the wait function would be to pause the execution of any scripts in the page for 500 ms, which would "freeze" things in an unresponsive state. Because of this, operations that take a long time to complete, like timeouts or AJAX requests usually use callbacks to signal when they are done instead of blocking execution and then returning when done.
By the way, when passing callbacks you should only pass the function name. If you add the parenthesis you are instead calling the function and passing its return value instead:
//When you write
foo(10, mycallback());
//You are actually doing
var res = mycallback();
foo(10, res);
//which will run things in the wrong order
Your code is not correct as Felix Kling already pointed out. Besides this, passing a function instead of calling one directly allows you to insert different behavior, your code is more decoupled and flexible. Here an example:
function sayBye(){
alert("Bye!");
}
function saySeeYou(){
alert("See you!");
}
function sayHello(name,myfunc){
alert("Hello");
if (myfunc) {
myfunc();
}
}
sayHello("Max",saySeeYou);
// I'm inserting a different behavior. Now instead of displayng "See you!"
// will show "Bye!".
sayHello("Max",sayBye);
You are doing it wrong, you should do like bellow
Don't call the function just pass the function as callback
use this
sayHello("Max",saySeeYou); //here the second parameter is function
instead of
sayHello("Max",saySeeYou());//This will put the result of saySeeYou as second parameter
in say hello call the functiom
function sayHello(name,myfunc){
console.log("Hello");
myfunc();
}

set time out not working with function

I am using the following to pause the javascript for a few seconds:
setTimeout(start_countdown(),3000);
It does not work, the function is called regardless of the seconds. The following function does however work, which doesnt use a function.
setTimeout(alert('hi'),3000);
How can I solve this?
You need to pass a function reference. You are passing a function's return value.
The difference is this: one is a blueprint of the function you want to happen, the other means you are executing the function immediately and passing its return value to setTimeout.
setTimeout(start_countdown, 3000);
If you want to do something more complex than simply call a named function, OR you want to pass a param to the named function, you'll need to instead pass an anonymous function to the timeout and call your function within that:
setTimeout(function() {
start_countdown(/* possible params */);
/* other code here as required */
}, 3000);
If you dont need to pass params dont use ()
setTimeout(start_countdown,3000);
If you do you have to wrap your function
setTimeout(function(){start_countdown(parameter)},3000);
write instead
setTimeout(start_countdown, 3000);
without parens ()
the second example could be also written as
setTimeout(function() { alert('hi'); }, 3000);
In different browsers it works in different way. In IE you need to use the anonymous function to pass the parameters to the callback:
setTimeout(function(){alert('hi')},3000);

Delay 6 seconds before beginning a function that will loop itself, small, not working, why?

I'm trying to create a 6 second delay before the heartColor(e) function begins, the function will then continue to loop. I don't understand why it's starting the function immediatley, and not waiting the 6 seconds it's supposed to, what did I do wrong?
function heartColor(e) {
e.animate({
color: '#7ea0dd'
}, 1000).animate({
color: '#986db9'
}, 1000).animate({
color: '#9fc54e'
}, 1000, function(){
heartColor(e)
})
}
$('.something').hover(function(){
setTimeout(heartColor($(this)), 6000);
})
The setTimeout() function expects its first parameter to be a function reference (or a string, but that's not recommended for several reasons). You are not passing it a function reference, you are calling the heartColor() function and passing the result to setTimeout(). So the function executes immediately, and then after six seconds nothing happens because the return value was undefined so setTimeout() had nothing to work with.
Try this instead:
$('.something').hover(function(){
var $this = $(this);
setTimeout(function() {
heartColor($this);
}, 6000);
})
The reason I have included an anonymous function as the parameter to setTimeout is that your call to heartColor() needs to pass a parameter through. If it didn't have any parameters you could do this:
setTimeout(heartColor, 6000);
Note there are no parentheses after heartColor - that gets a reference to the function without calling it so that later setTimeout calls it for you. But you can't get a reference to the function and provide parameters at the same time so you need to wrap the call up in another function. You could do this:
var $this = $(this);
function callHeartColor() {
heartColor($this);
}
setTimeout(callHeartColor, 6000);
My original answer with the anonymous function is kind of short hand for that and (most people find it) more convenient.
The reason I have created a variable $this is because of the way the this keyword works in JavaScript, which depends on how a function is called. If you simply said heartColor($(this)) inside the anonymous function (or the callHeartColor() function) this would not be the element being hovered over.
you are invoking the function heartColor instead of passing it as a parameter. you have to do:
$('.something').hover(function(){
setTimeout(function(){heartColor($(this))}, 6000);
})
You want this:
$('.something').hover(function(){
setTimeout(function() {heartColor($(this));}, 6000);
})

setTimeout waits when passing the code but not when passing reference

Why when passing the code (surrounded by '') like so:
setTimeout('alert("James Blunt is bad")', 5000);
Is there the 5 second pause before showing what we all know and when passing function reference like so:
setTimeout(alert("James Blunt is bad"), 5000);
There is no pause?
http://jsfiddle.net/eBcpc/
If your second example, you are not passing a function.
You are calling alert and passing its return value (and the return value of alert is not a function).
Function references don't end in (…)
setTimeout(function () { alert("Hello, world"); }, 5000);
Or, with weaker browser support, pass the arguments to alert in an array as the third argument to setTimeout.:
setTimeout(alert, 5000, ["Hello, world"]);
When you call setTimeout with a code argument, you have to wrap it in a function:
setTimeout(function () { alert("James Blunt is bad"); }, 5000);
Otherwise, when JavaScript executes setTimeout, it will run the alert function instantaneously in the hopes that the alert will return a function object which can then be enqueued onto the timeout queue. (Obviously, alert will not return a function.)
In the second example, you are invoking the alert function, then calling setTimeout. This is because the arguments to a function are processed before the function is called. (If alert had a return value and you wanted to pass that value as an argument to another function, then this is exactly the behavior you would want.)
What you want to do is pass an anonymous function to setTimeout, then have that anonymous function call alert, like so:
setTimeout(function() {
alert("James Blunt is bad");
}, 5000);

javascript settimeout cleartimeout routine syntax

This is the code from another thread. It activates a function only when the user has stopped typing after a set time.
var keyupTimer;
function keyUpEvent(){
clearTimeout(keyupTimer);
keyupTimer = setTimeout(sendInput,1000); // will activate when the user has stopped typing for 1 second
}
function sendInput(){
alert("Do AJAX request");
}
It works as is. But why does it stop working if I put parenthesis to try to pass variables in this line:
keyupTimer = setTimeout(sendInput,1000); //original code
To
keyupTimer = setTimeout(sendInput(),1000); //with just empty ()
or
keyupTimer = setTimeout(sendInput(var),1000);//or with ('test') or with (var)
with the parenthesis, the delay does not occur and the sendInput function is called immediately. Is this the only format for this particular routine?
TIA
keyupTimer = setTimeout(sendInput,1000); //original code
This says "Run sendInput after 1000ms";
keyupTimer = setTimeout(sendInput(),1000); //with just empty ()
This says "Run sendInput, capture the return value (which should be a function) and run that after 1000ms".
sendInput is a function, sendInput() is a function call.
The first argument for setTimeout is a function reference (i.e. a variable that points to your function). What you provided is a function call. To pass a function with arguments, wrap the function call in an anonymous function. You can pass it directly to setTimeout as an argument since JavaScript functions are first-class objects.
keyupTimer = setTimeout(function() {
sendInput(var);
}, 1000);
In more verbose, that equals to this:
var callback = function() {
sendInput(var);
}
keyupTimer = setTimeout(callback, 1000);
The inline pattern has its advantage that it has access to the scope where the setTimeout is called.
If it fits better to you, you could even create a callback factory to pass a function call to setTimeout as #slebetman pointed out.
function callbackFactory (var) {
return function() {
sendInput(var);
}
};
setTimeout(callbackFactory('some_value'), 1000);
you can try
keyupTimer = setTimeout(function()
{
sendInput('test');
},1000);
so have an anonymous function as parameter for 'setTimeout'
it is also possible to use a string containing javascript code, as a first argument to setTimeout. however, it is strongly discouraged (see comments)
...
/* DON'T DO THIS: */
keyupTimer = setTimeout("sendInput(variable)", 1000)
...

Categories