Javascript close function not returning correct value

I have a problem with Javascript code and don't know why it won't run.

This is my timer:

var time = 0;
startTimer();

function startTimer(){
    setInterval(function(){
    time=time+1;
    },10);
}

      

This code works here (I run timeoutTester () by clicking on the button, then it alerts and shows me the time difference):

function timeoutTester(){
    var snap_time1 = time;
    setTimeout(function(){
        var snap_time2 = time;
        var diff = snap_time2-snap_time1;
        alert(diff);            //works: ~100 everytime...
    },1000);
}

      

But this code doesn't work here (I run testTimer () by clicking a button):

function timeoutTester(){
    var snap_time1 = time;
    var result;
    setTimeout(function(){
        var snap_time2 = time;
        result = snap_time2-snap_time1;
    },1000);
    alert(result); //doesn't work! It always shows me: "undefined"
    return result;
}

function testTimer(){
    var counter = 0;
    setInterval(function(){
        counter = counter + 1;
        alert(counter);         //works: 1,2,3,....
        var result = timeoutTester();
        alert(result);  //doesn't work! It always shows me: "undefined"
    }, 3000);
}

      

Do you know where the problem might be? (Error while debugging in browser!)

UPDATE

I found out that warning (result); is executed immediately, as long as the result does not matter yet.

But since I'm calling timeoutTester () from outside, I still need to return the value from this function!

Any ideas?

+3


source to share


2 answers


To do this, you need to use the global:

var timeoutTesterResult;

function timeoutTester(){
    var snap_time1 = time;
    var result;
    setTimeout(function(){
        var snap_time2 = time;
        timeoutTesterResult = snap_time2-snap_time1;
        alert(timeoutTesterResult);                    //works: ~100
    },1000);
}

      

And the second timeout that this global only gets after setting / changing it:



function testTimer(){
    var counter = 0;
    setInterval(function(){
        counter = counter + 1;
        alert(counter);         //works: 1,2,3,....
        timeoutTester();
        setTimeout("alert(timeoutTesterResult)",1000); //works: ~100
    }, 3000);
}

      

This is a "global" solution.

+1


source


In the last example (the one that doesn't work), the call setTimeout(function()...)

does not execute the function immediately, but returns immediately. When it returns, it result

's still undefined because the function hasn't been executed yet.

In the same way, the previous example (which has a call alert()

inside a timeout function) works because the call alert()

occurs after the result

value is assigned.

If for some reason you don't want to call alert()

directly from the timeout function, you can rewrite the last example as follows:



function timeoutTester(){
    var snap_time1 = time;
    setTimeout(function(){
        var snap_time2 = time;
        alertResult(snap_time2-snap_time1);
    },1000);
}
function alertResult(result) {
    alert(result);
}

      

(By the way, the call startTimer(1000000000000);

looked scary until I realized that the argument was being ignored. :-).)

+4


source







All Articles