javascript/google Hangouts api 中的回调函数
所以我在搞乱谷歌的hangouts api,有一个名为 addStateChangeListener( 打回来 ) 它允许您注册一个回调函数,每当应用程序的状态发生变化时就会调用该函数。
可以注册的示例回调函数是
function onStateChanged(add, remove, state, metadata) {
state_ = state;
metadata_ = metadata;
if (<some boolean>) {
doFunction(); //this function alters the state
}
//more stuff below
}
我的问题是:如果 doFunction() 做了一些改变状态的事情(并触发了 addStateChangeListener),在 if 语句运行后,在函数的其余部分之前是否会再次调用 onStateChange ? 或者 onStateChange() 的第一次迭代首先运行完成,然后再次调用 onStateChange。 或者它可能会完全忽略第一个 onStateChange 函数的其余部分,并在 doFunction 更改状态时调用 onStateChange ?
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Google Hangouts API 参考,它似乎会再次运行回调函数如果你改变了状态。具体来说:
它肯定不会忽略第一个回调函数的其余部分,并且它可能(尽管可能不会,例如,如果它通过 Ajax 调用与服务器检查状态实际上已更改)也会立即运行第二个回调函数,而无需等待第一个回调函数完成。如果您想确保第一个函数始终在调用第二个函数之前完成,则可以始终在调用
doFunction() 周围使用
。指定 1 毫秒的延迟就足够了。window.setTimeout()
延迟调用以更改状态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.