Javascript:全局变量不起作用。 (使用SDK.REST.retriveMultipleRecords) [英] Javascript: global var doesn't work. (using SDK.REST.retriveMultipleRecords)

查看:119
本文介绍了Javascript:全局变量不起作用。 (使用SDK.REST.retriveMultipleRecords)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我不能影响我的全局变量。我不明白为什么这不起作用..我相信它与SDK.REST.retriveMultipleRecords有关,但我不明白如何。正如你可以在第一次警报中看到的那样。但在第二个显示为未定义。
在这个小提琴中,我有一个关于定义一个全局变量的简单例子,它可以工作。

I don't understand why i can't affect my global variable. I don't understand why this doesn't work.. I believe that it has to do with SDK.REST.retriveMultipleRecords, but i dont understand how. As you can see in first alert it works. but in 2nd it shows as "Undefined". In this fiddle, i have a simple example about defining a global variable and it works.

任何人都可以帮助解决这个问题吗?为什么它不影响我的globalVar?

Can anyone help with this issue? Why it is not affecting my globalVar?

Players = new Array(); //GLOBAL VAR

function setPlayers(topNumber){

  SDK.REST.retrieveMultipleRecords(

    "gamify_utilizador",

    "$select=gamify_utilizadorId,gamify_name,gamify_Ranking&$top="+topNumber+"&$orderby=gamify_Ranking asc",

    function (results) {

      if(results.length > 0){
          for(var i = 0;i<results.length;i++){

              Players.push(new Array(results[i].gamify_utilizadorId, results[i].gamify_name));
              alert(Players[i]);  // ALERT OK
          }
      }
      else {
          alert("No Contact records are available to set as the primary contact for the account."); 
      }

    },

    errorHandler,

    function () { 
        //OnComplete handler
    }
  )
  alert(Players[1]); // Alert says "Undefined"

};
document.onreadystatechange = function () {
    if (document.readyState == "complete") {
        setPlayers(4);
       alert(Players[1]);
    }
}


推荐答案

think它应该是因为内部函数尚未执行,因此该数组在位置1仍然不包含任何值,只是在该数组的执行和完成之后才会对此可用值进行赋值

think it should be because the inner function has not yet been performed, and thus the array still contains no value at position 1, just after the execution and completion of this array will value this available

这篇关于Javascript:全局变量不起作用。 (使用SDK.REST.retriveMultipleRecords)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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