So I was messing around with google’s hangouts api and there is a function called addStateChangeListener(
callback
)
which allows you to register a callback function that will be called whenever the state of the application changes.
An example callback function that could be registered is
function onStateChanged(add, remove, state, metadata) {
state_ = state;
metadata_ = metadata;
if (<some boolean>) {
doFunction(); //this function alters the state
}
//more stuff below
}
My question is: If doFunction() did something that altered the state, (and triggered the addStateChangeListener) would onStateChange be called again before the rest of function after the if statement ran?
Or would the first iteration of onStateChange() run to its completion first and then onStateChange would get called again.
Or would it possibly just completely ignore the rest of the first onStateChange function, and just recall onStateChange when doFunction changes the state?
Thanks for your help.
Looking at the Google Hangouts API reference, it would appear that it would run the callback function again if you changed the state. Specifically:
It would definitely not ignore the rest of the first callback function, and it would probably (although it may not, for example, if it checks with the server via an Ajax call that the state has actually changed) also run the second immediately, without waiting for the first callback function to finish. If you wanted to ensure that the first function always completes before the second is called, you could always delay your call to change the state with
window.setTimeout()around the call todoFunction(). Specifying a 1 millisecond delay will be enough.