LoadVars不会等到数据加载完毕

时间:2021-01-18 21:12:52
variable = 0;

function change() {
 variable = 1;
}

go = newVars(); 
go.onLoad = function(success) {
 trace("#First");
 change();
}
go.load('http://www.*.com/variables'); 

trace("#Second");
trace(variable); // output = 0;

The problem is that it executes #Second before #First, which means that the object is not fullly loaded but the code continues nonetheless. Is there a way handle this? I have tried using a while loop, but this is ugly and makes flash crash. Is there any decent way to handle this, does it have to do with better code structure/program flow or is there a technical way to make it wait? Also note: This code is executed on the server side, which means there are no frames involved.

问题是它在#First之前执行#Second,这意味着对象没有完全加载但是代码仍在继续。有办法解决这个问题吗?我尝试过使用while循环,但这很难看并且会导致闪存崩溃。有没有合适的方法来处理这个问题,它是否与更好的代码结构/程序流程有关,还是有技术方法让它等待?另请注意:此代码在服务器端执行,这意味着不涉及任何帧。

UPDATE: When projects get bigger, this gets very ugly, especially when you are retrieving mulitple things from a server, you have to use very deep nesting, you have to keep repeating the same code, example for buying a serial:

更新:当项目变大时,这变得非常难看,特别是当你从服务器检索多种东西时,你必须使用非常深的嵌套,你必须不断重复相同的代码,例如购买序列:

a.onload() {


 if(moneyAmount > 10){

  b.onload(pay) {

   sendProduct();

   d.onload(serial) {

    print(serial);

   }

   d.load('www.so.com/getSerial');

}
  b.load('www.so.com/pay?amount=5');

 }else{

  c.onload(wallet) {

   displayWallet(wallet)

  }

  c.load('www.so.com/getWallet);

 }

}
a.load('www.so.com/checkMoneyAmount');

1 个解决方案

#1


It shouldn't need to wait, that's the whole reason why they expose the 'onLoad' event. Whatever code you wanted to have execute after it has fully loaded you should put inside the onLoad function.

它不应该等待,这就是它们暴露'onLoad'事件的全部原因。无论您想要在完全加载后执行什么代码,都应该放入onLoad函数。

-- edit --

- 编辑 -

That's just the nature of the beast. Javascript is not threaded, and you don't know when your web service calls will return, so you have to use an asynchronous model.

这只是野兽的本质。 Javascript没有线程,你不知道你的Web服务调用什么时候会返回,所以你必须使用异步模型。

var bank = {
    onGetSerial: function(s) {
        print(serial);
    },

    onPay: function(pay) {
        sendProduct();
        d.load('www.so.com/getSerial');
    },

    onGetWallet: function(wallet) {
        displayWallet(wallet);
    },

    onCheckMoneyAmount: function(moneyAmount) {
        if (moneyAmount > 10) {
            b.load('www.so.com/pay?amount=5');
        } else {
            c.load('www.so.com/getWallet');
        }
    }
};

a.onLoad = bank.onCheckMoneyAmount;
b.onLoad = bank.onPay;
c.onLoad = bank.onGetWallet;
d.onLoad = bank.onGetSerial;

a.load('www.so.com/checkMoneyAmount');

#1


It shouldn't need to wait, that's the whole reason why they expose the 'onLoad' event. Whatever code you wanted to have execute after it has fully loaded you should put inside the onLoad function.

它不应该等待,这就是它们暴露'onLoad'事件的全部原因。无论您想要在完全加载后执行什么代码,都应该放入onLoad函数。

-- edit --

- 编辑 -

That's just the nature of the beast. Javascript is not threaded, and you don't know when your web service calls will return, so you have to use an asynchronous model.

这只是野兽的本质。 Javascript没有线程,你不知道你的Web服务调用什么时候会返回,所以你必须使用异步模型。

var bank = {
    onGetSerial: function(s) {
        print(serial);
    },

    onPay: function(pay) {
        sendProduct();
        d.load('www.so.com/getSerial');
    },

    onGetWallet: function(wallet) {
        displayWallet(wallet);
    },

    onCheckMoneyAmount: function(moneyAmount) {
        if (moneyAmount > 10) {
            b.load('www.so.com/pay?amount=5');
        } else {
            c.load('www.so.com/getWallet');
        }
    }
};

a.onLoad = bank.onCheckMoneyAmount;
b.onLoad = bank.onPay;
c.onLoad = bank.onGetWallet;
d.onLoad = bank.onGetSerial;

a.load('www.so.com/checkMoneyAmount');