Wait until function is finished

estradowiec

What is a best way to execute function until before call is finished?

Example JavaScript function:

var notifyState = function(state) {
setTimeout(function () {
  /*Do something*/
}, 2000);
};

Then I call function twice:

notifyState('State1');
notifyState('State2');

This functions executing in paraller. What best way to execute them sequentially? I can only modify notifyState function. notifyState can execute only with one parameter.

Update: Function notifyState informs what actual state is in flash game. It saves it in html code, when state change then state is override. In selenium test I downloading state from html code, but state change too fast that selenium not noticed this, so I tried sleep JavaScript.

edzhelyov

It depends on whether you want #notifyState to use setTimeout or not. If you don't need the setTimeout then you just re-write as follows:

var notifyState = function(state) {
  /* Do something */
}

notifyState('State 1');
notifyState('State 2');

If you want to keep the setTimeout and chain the calls, so that the second notify is executed after the first, you will have to provide a callback:

var notifyState = function(state, callback) {
  setTimeout(function() {
    /* Do something */
    if (callback) {
      callback();
    }
  }, 2000);
}

notifyState('State 1', function() {
  notifyState('State 2');
});

EDIT

Seems that the OP problem is different. My understanding is that your are providing #notifyState function as a callback to a 3rd party Flash that you don't control and you want to ensure that calls to notifyState execute in the same order and one after another, so you don't have 2 parallels calls to notifyState running at the same time. To achieve this you will need to introduce a queue that will keep the states and change the notifyState function in a way to execute only one state at the time. I will assume your need for the setTimeout is important here and keep it that way. See the code below:

var Queue = [],
    running = false;
var notifyState = function(state) {
  if (running) {
    Queue.push(state);
  } else {
    running = true;
    setTimeout(function() {
      /* Do something */
      running = false;
      var nextState = Queue.pop();
      if (nextState) {
        notifyState(nextState);
      }
    }, 2000);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can a for-loop wait until a function within it has finished executing?

From Dev

Wait until forEach loop is finished?

From Dev

Wait until gobalEval has finished

From Dev

On click myFunction and wait until it is finished

From Dev

Wait until NSURLConnection sendAsynchronousRequest is finished

From Dev

Android AsyncTask wait until finished

From Dev

Wait until my thread is finished

From Dev

Wait until gobalEval has finished

From Dev

Wait until process is finished running

From Java

How to wait until all async calls are finished

From Dev

Sequelize wait until loop finished with callback

From Dev

How to wait until an animation is finished in Swift?

From Dev

How to wait until all NSOperations is finished?

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

jQuery mobile: Wait until $.getJSON is finished

From Dev

looping for jquery load and wait until finished

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

Make Excel VBA wait until operation is finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

How to pick an event listener that will let me wait until async.times is finished to run a function

From Dev

javascript wait til function is finished

From Java

Javascript Pause Loop Until function is finished

From Dev

How to wait until all tasks are finished before running code

From Dev

Why the main does not wait until the async method has finished?

Related Related

  1. 1

    Can a for-loop wait until a function within it has finished executing?

  2. 2

    Wait until forEach loop is finished?

  3. 3

    Wait until gobalEval has finished

  4. 4

    On click myFunction and wait until it is finished

  5. 5

    Wait until NSURLConnection sendAsynchronousRequest is finished

  6. 6

    Android AsyncTask wait until finished

  7. 7

    Wait until my thread is finished

  8. 8

    Wait until gobalEval has finished

  9. 9

    Wait until process is finished running

  10. 10

    How to wait until all async calls are finished

  11. 11

    Sequelize wait until loop finished with callback

  12. 12

    How to wait until an animation is finished in Swift?

  13. 13

    How to wait until all NSOperations is finished?

  14. 14

    wait until completion block not finished in AFJSONRPCClient request

  15. 15

    how to wait until a web request with HttpWebRequest is finished?

  16. 16

    How to wait until some jQuery methods are finished?

  17. 17

    How to wait until networking by Alamofire has finished?

  18. 18

    How to wait until my batch file is finished

  19. 19

    jQuery mobile: Wait until $.getJSON is finished

  20. 20

    looping for jquery load and wait until finished

  21. 21

    wait until completion block not finished in AFJSONRPCClient request

  22. 22

    Make Excel VBA wait until operation is finished

  23. 23

    How to get jQuery to wait until an animation is finished?

  24. 24

    Android - How to wait until a SnapshotListener has finished?

  25. 25

    How to pick an event listener that will let me wait until async.times is finished to run a function

  26. 26

    javascript wait til function is finished

  27. 27

    Javascript Pause Loop Until function is finished

  28. 28

    How to wait until all tasks are finished before running code

  29. 29

    Why the main does not wait until the async method has finished?

HotTag

Archive