javascript/google Hangouts api 中的回调函数

发布于 2024-12-10 09:36:15 字数 619 浏览 1 评论 0原文

所以我在搞乱谷歌的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-12-17 09:36:15

查看 Google Hangouts API 参考,它似乎会再次运行回调函数如果你改变了状态。具体来说:

共享状态发生变化时将调用回调函数
来自本地参与者的应用程序进行的 SubmitDelta 调用的结果。

它肯定不会忽略第一个回调函数的其余部分,并且它可能(尽管可能不会,例如,如果它通过 Ajax 调用与服务器检查状态实际上已更改)也会立即运行第二个回调函数,而无需等待第一个回调函数完成。如果您想确保第一个函数始终在调用第二个函数之前完成,则可以始终在调用 doFunction() 周围使用 window.setTimeout() 延迟调用以更改状态。指定 1 毫秒的延迟就足够了。

Looking at the Google Hangouts API reference, it would appear that it would run the callback function again if you changed the state. Specifically:

the callback will be called for changes in the shared state which
result from submitDelta calls made from the local participant's app.

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 to doFunction(). Specifying a 1 millisecond delay will be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文