javascript函数同步
javascript function synchronization
我有这样的东西:
<html>
<script type="text/javascript">
//BLOCK1
function startingFunction() {
//do a server call, receive the response through callback functions, either successFunction or failFunction
onSuccess : successFunction,
onFailure : failFunction
}
function successFunction(result) {
//called receiving the server response
//configure something based on success
}
function failFunction(result) {
//called receiving the server response
//configure something based on failure
}
</script>
<script type="text/javascript">
// BLOCK2
//javascript code that needs to wait for the result of the configuration made above in BLOCK1 in order to continue doing more stuff
</script>
<body>...</body>
</html>
我如何让BLOCK2中的javascript代码等待服务器做出响应并进行配置?
您可以简单地从第一个块中可以调用的两个函数中调用第二个块中的一个函数。(假设你想在失败后继续)
<script type="text/javascript">
//BLOCK1
function startingFunction() {
//do a server call, receive the response through callback functions, either successFunction or failFunction
onSuccess : successFunction,
onFailure : failFunction
}
function successFunction(result) {
//called receiving the server response
//configure something based on success
afterConfiguration();
}
function failFunction(result) {
//called receiving the server response
//configure something based on failure
afterConfiguration();
}
</script>
<script type="text/javascript">
// BLOCK2
//javascript code that needs to wait for the result of the configuration made above in BLOCK1 in order to continue doing more stuff
function afterConfiguration(){
//code that needed to wait
}
</script>
只需将代码放在函数内的块2中,然后在successFunction
结束时调用该函数。
再说一遍,我不认为把它分成两个script
元素有什么意义。
您也可以使用标志并使用setInterval()定期检查:
使用标志:
<html>
<script type="text/javascript">
//BLOCK1
var block1Success = false; // <-- this is our flag
function startingFunction() {
//do a server call, receive the response through callback functions, either successFunction or failFunction
onSuccess : successFunction,
onFailure : failFunction
}
function successFunction(result) {
//called receiving the server response
//configure something based on success
block1Success = true; // <-- flag is set here
}
function failFunction(result) {
//called receiving the server response
//configure something based on failure
}
</script>
<script type="text/javascript">
// BLOCK2
//javascript code that needs to wait for the result of the configuration made above in BLOCK1 in order to continue doing more stuff
window.setInterval(
function(){
if(block1Success){
// code need to be executed here..
alert("block1 success executed");
}
}, 100)
</script>
<body>...</body>
</html>
相关文章:
- Chrome扩展和Angular:我可以把Chrome.runtime.sendMessage变成一个同步函数吗
- 将同步函数包装到承诺中的最佳方法是什么?
- javascript中同步函数执行中的问题
- 同步函数调用涉及post-json调用,其中一个函数应该在另一个函数成功后成功
- 如何从异步函数创建同步函数以实现向后兼容性
- 在 Node.js 中何时使用同步函数是否有约定
- JavaScript 中的同步函数
- 在 async.each 的帮助下调用 foreach 中的同步函数(带回调)
- 如何确保同步函数的执行发生在 3 个异步函数完全完成后
- AJAX / jQuery / etc.库之前的JavaScript同步函数 - 不记得实现,任何建议
- 在javascript中制作同步函数
- 使用Jquery$When进行异步和同步函数调用
- javascript中返回错误的纯同步函数
- 如何使与jquery延迟对象一起工作的函数表现得像同步函数
- 与异步和同步函数相关的未定义javascript函数的问题
- 在node.js中编写同步函数的简单方法
- Javascript同步函数-chrome扩展
- 如何在nodejs中同步函数调用
- 如何在angular js中同步函数命令
- 在JavaScript中的异步函数末尾运行同步函数