3

How can I use setTimeout if I want to return a value

$.each(pCodes, function(index, pCode) {
    setTimeout(func(parm1), 2000);      
});


function func(in)
{
  var value = 999;
  return value;
}
5
  • How would you use this return value? It doesn't seem to make much sense. Commented Aug 22, 2011 at 0:10
  • To what do you want to return that value? You have no idea what will be running when the timeout happens. Commented Aug 22, 2011 at 0:11
  • I want to call function func every 2 seconds and return a value on each call. Commented Aug 22, 2011 at 0:12
  • Your question makes no sense. What do you want to return the value to? Commented Aug 22, 2011 at 0:15
  • But return a value to where. See my answer for the pattern that is generally used for 'returning' a value from a timeout. Commented Aug 22, 2011 at 0:15

4 Answers 4

12

First of all, your call to setTimeout is wrong. You are calling the function func and then using the result in the setTimeout method. Your code is equivalent to:

$.each(pCodes, function(index, pCode) {
  var temp = func(parm1);
  setTimeout(temp, 2000);      
});

As func returns 999, you will be doing setTimeout(999, 2000), which of course doesn't make sense. To call a function that takes a parameter from setTimeout you need a function that makes that function call:

$.each(pCodes, function(index, pCode) {
  setTimeout(function() { func(parm1); }, 2000);
});

To handle the return value from func is a bit more complicated. As it's called later on, you have to handle the return value later on. Usually that is done with a callback method that is called when the return value is available:

var callback = function(value) {
  // Here you can use the value.
};
$.each(pCodes, function(index, pCode) {
  setTimeout(function() { func(parm1, callback); }, 2000);
});

function func(in, callback) {
  var value = 999;
  callback(value);
}
Sign up to request clarification or add additional context in comments.

Comments

3

First of all, make sure you pass to setTimeout a function, in your example you passed undefined to it, as you func(param1) executes func directly. What you want is something like this:

setTimeout(function() { func(parm1); }, 2000);

And for 'returning' the value: Use some kind of callback function that is executed with the value when timeout expired. Like so:

function callback(value) {
  //  doSomethingWithNewValue
}

$.each(pCodes, function(index, pCode) {
    setTimeout(function() { func(parm1, callback); }, 2000);      
});


function func(in, callback)
{
  var value = 999;
  callback(value);
}

This is the general pattern used in such scenario (see event driven programming).

2 Comments

The idea is good and correct but setTimeout(func(parm1, callback), 2000); is wrong as it will get evaluated immediatly. You want to do something like setTimeout(function(){func(parm1, callback);}, 2000);
When you call func(parm1, callback), is callback a variable or the name of the function?
0

change it to :

var defValue;

$.each(pCodes, function(index, pCode) {
    setTimeout(function(){defValue=func(parm1)}, 2000);      
});

this way you can use the defValue in your function to access the returned value

Comments

0

It's pretty ugly, but you can use output parameters, since js objects are pass by reference:

function a() {
    var param1 = 42;
    var result = {};
    b(param1, result);
}

function b(val, output) {
    something();
    output.returned = 4;
}

Or, you can use a callback (the better option):

function a() {
    var param1 = 42;
    b(param1, function (newVal) {
        something();
    });
}

function b(val, callback) {
    //something
    callback(4);
}

By the way, your call to setTimeout is wrong. setTimeout receives a function as a first parameter, and a delay as a second - the first argument is still seen as regular javascript code, so it evaluates it, so your setTimeout call turns out to be like this:

setTimeout(999, 2000);

Since you're returning 999 from the function.

However, setTimeout can also receive a list of arguments after the second parameter, so it can be turned into this:

setTimeout(func, 2000, param1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.