使用Javascript等功能响应 [英] Javascript wait for function response

查看:106
本文介绍了使用Javascript等功能响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

  myFunc的();
酒吧();
 

myFunc的()正在一个Ajax请求

我不希望执行杆(),直到myFunc的()的要求已经完成。

我也不想动调用bar()myFunc的内部。

可能吗?

修改

下面是code我结束了:

  VAR FOO = {
 初始化:功能(废话)
 {
  //回调传递到了AJAX的挑战数据加载
  VAR回调= {
   myFunc1:函数(){myFunc1(等等); },
   myFunc2:函数(){myFunc2(等等); },
  };

  this.bar(回调); //将挑战数据和设置游戏
 },
 巴:函数(回调){..loop通过并执行它们..}

};
 

解决方案

为了做你在找什么,你必须找到一种方法的酒吧()的用的沟通myFunc的()的。

当你说你不希望将呼叫转移到的酒吧()的里面的 myFunc的()的这可能是PTED在几个方面除$ P $

例如,你可以做的酒吧()的的参数的 myFunc的()

 函数bar(){
   //做吧()做
}

功能myFunc的(回调){
   //利用回调在xmlht prequest对象的onreadystatechange属性
}

myFunc的(巴)
 

我希望这会帮助你。

杰罗姆·瓦格纳

I have the following code:

myFunc();
bar();

myFunc() is making an ajax request

I don't want to execute bar() until myFunc()'s request has completed.

I also do not want to move the call to bar() inside of myFunc.

possible?

EDIT

Here is the code I ended up with:

var FOO = {
 init: function(blah)
 {
  // Callbacks to pass to the AJAX challenge data load
  var callbacks = {
   myFunc1: function(){ myFunc1(blah); },
   myFunc2: function(){ myFunc2(blah); },
  };

  this.bar(callbacks); // Load the challenge data and setup the game
 },
 bar: function(callbacks) { ..loop through and execute them.. }

};

解决方案

In order to do what you are looking for, you must find a way for bar() to communicate with myFunc().

When you say that you do not want to move the call to bar() inside myFunc() this can be interpreted in several ways.

For example, you could make bar() a parameter of myFunc()

function bar() {
   // do what bar() does
}

function myFunc(callback) {
   // use callback in the onreadystatechange property of the xmlhtprequest object
}

myFunc(bar)

I hope this will help you

Jerome Wagner

这篇关于使用Javascript等功能响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆