4

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.

asked Jan 8, 2014 at 12:12
8
  • 9
    time for Callback Commented Jan 8, 2014 at 12:13
  • First of all... there's no multithreading in javascript so your function will always be run sequentially but you can't tell in which order Commented Jan 8, 2014 at 12:15
  • @PraveenJeganathan: Callback isn't going to work if OP can only modify notifyState function Commented Jan 8, 2014 at 12:15
  • 1
    @oGeez - Yes, I can create helper function to call notifyState Commented Jan 8, 2014 at 12:20
  • 1
    @SOReader: there is multithreading in JavaScript, see web workers: developer.mozilla.org/en-US/docs/Web/Guide/Performance/… Commented Jan 8, 2014 at 12:31

3 Answers 3

6

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);
 }
}
answered Jan 8, 2014 at 12:25
Sign up to request clarification or add additional context in comments.

1 Comment

I can execute notifyState only with one parameter (actual state). I don't know what state will be next.
2

Add an optional parameter that will act as a callback function. Check that the parameter is in fact a function, if it is, run it.

var notifyState = function(state,fn) {
 setTimeout(function () {
 /*DoSomething*/
 if(typeof(fn)=='function'){ fn(); }
 }, 2000);
};

You can then call the functions like so:

notifyState('State1', function(){
 notifyState('State2');
});

JSFiddle

answered Jan 8, 2014 at 12:21

9 Comments

but Op mentioned can only modify notifyState function. So I think OP can't use it
So the OP can modify the function itself but not the call to the function?
based on the question it seems like that, only OP can clarify it.
I may have misunderstood, thanks for noting - I will wait for clarification.
It's work but not for me. I get from Flash application external call only with one parameter (actual state). I don't know which state will be next. Maybe use something like locker?
|
0
var notifyState = function(state, callback) {
 setTimeout(function () {
 /*Do something*/
 callback();
 }, 2000);
};

Calling function:

var state1CompleteHandler = function() {
 // code to execute when state 1 logic completes
 notifyState('State2');
}
notifyState('State1', state1CompleteHandler);
answered Jan 8, 2014 at 12:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.