1

I have a simple count function increments a number into an input field. This works exactly how I would like it to work. The only problem is, is that i have a long JS script with multiple function and I only want the script to continue runnning when the count() function is complete.

JSFIDDLE http://jsfiddle.net/vyN6V/243/

var number = 1500;
var aValue = 300;

function count(Item) {
    var current = aValue;
    Item.val(aValue += 1);
    if (current < number) {
        setTimeout(function () {
            count(Item)
        }, 0.1);
    }
}

count($(".input"));

// the rest of the script should only run when the above function has completed

$('span').text('code here should only run when function count is complete');
2
  • Why do you need an asynchronous call ? Commented Feb 24, 2014 at 17:55
  • FYI, delay parameter of setTimeout is only an integer, not a float number, though, you could ommit it Commented Feb 24, 2014 at 17:56

7 Answers 7

3

What you need is a callback. I'm sure you have used callbacks, for example in jQuery.

Here is how to add it to your code:

function count(Item, Callback) {    // new argument
    var current = aValue;
    Item.val(aValue += 1);
    if (current < number) {
        setTimeout(function () {
            count(Item, Callback)   // pass it in recursive calls 
        }, 0.1);
    } else {
        Callback();                 // call it at the end of the loop
    }
}

count($(".input"), function() {     // write the callback code here
    // this will be executed after the counts
});
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be it, just testing with my script - never thought of call back.
0

setTimeout by design runs asynchronously so just by calling it you are allowing your code to continue processing and come back to the setTimeout code when it's ready (it decides when, not you).

In order to do this in JavaScript you need to encapsulate the rest of your code in a function that you don't invoke until the count function is done.

May I ask why you are using setTimeout in this fashion? It seems like what you want is actually a synchronous function so why not simply use a normal while statement in your count() function?

1 Comment

The only reason is that he might want to animate the number being changed.
0

Try this: http://jsfiddle.net/aamir/vyN6V/248/

var number = 1500;
var aValue = 300;

function count(Item) {
    var current = aValue;
    Item.val(aValue += 1);
    if (current < number) {
        setTimeout(function () {
            count(Item)
        }, 0.1);
    } else {
        callback();
    }
}

count($(".input"));

function callback() {
    $('span').text('code here should only run when function count is complete');
}

Comments

0

If you can remove the asynch call :

var number = 1500;
var aValue = 300;

function count(Item) {
    var current = aValue;
    while(current <= number){
        Item.val(aValue += 1);
        current = aValue;

    }
}

count($(".input"));

Comments

0
 var number = 1500;
    var aValue = 300;
     function count(Item) {
            var current = aValue;
            Item.val(aValue += 1);
            if (current < number) {
                setTimeout(function () {
                    count(Item)
                }, 0.1);
            }
            if (current == number) {
                $('span').text('code here should only run when function count is complete');
            }
        }

        count($(".input"));

Comments

0

There is the need of asynchronous handling, invoke the callback function restFunctionCalled() when you need it. something like this.

var number = 1500;
var aValue = 300;

function count(Item) {
    var current = aValue;
    Item.val(aValue += 1);
    if (current < number) {
        setTimeout(function () {
            count(Item)
        }, 0.1);
    }else{
        restFunctionCalled(); //invoke the callback function from here.
    }
}

count($(".input"));

function  restFunctionCalled(){
     $('span').text('code here should only run when function count is complete');
}

DEMO

Comments

0

There are two options for you simulate sleep or encapsulate the rest of the code and call you when your function is finished(being more advisable the second option.).

function sleep(milliSeconds){
var startTime = new Date().getTime(); // get the current time
while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

or

function callFunction(){
   $('span').text('code here should only run when function count is complete');
}

if (current < number) {
    setTimeout(function () {
        count(Item)
    }, 0.1);
}
else{
   callFunction()
}

Comments

Your Answer

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