Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我尝试在下一行代码中使用已初始化的变量时,该变量未定义_Javascript_Jquery_Web - Fatal编程技术网

Javascript 当我尝试在下一行代码中使用已初始化的变量时,该变量未定义

Javascript 当我尝试在下一行代码中使用已初始化的变量时,该变量未定义,javascript,jquery,web,Javascript,Jquery,Web,我正在开发一个大量使用Javascript和jQuery的web应用程序 我基本上是在网页加载时创建一个app对象,并在页脚中调用init方法 var app = new App(); app.init(); 这些是init方法中的第一行代码 下面是loadIndex方法的几行代码 loadIndex方法基本上是加载一个约135kb的json文件,并将数据作为对象加载到hashtag_index变量。hashtag_索引变量是全局定义的,因此可以全局访问 变量hashtag索引有8个不同的对象

我正在开发一个大量使用Javascript和jQuery的web应用程序

我基本上是在网页加载时创建一个app对象,并在页脚中调用init方法

var app = new App();
app.init();
这些是init方法中的第一行代码

下面是loadIndex方法的几行代码

loadIndex方法基本上是加载一个约135kb的json文件,并将数据作为对象加载到hashtag_index变量。hashtag_索引变量是全局定义的,因此可以全局访问

变量hashtag索引有8个不同的对象数组。但是当我试图警告变量时,变量还没有初始化

但警报只返回一条空白消息。当我将此作为消息返回时:

alert(typeof(hashtag_index))
我没有被定义为消息

但是现在我确定变量已经加载了,因为当我通过谷歌浏览器的控制台访问变量时,我可以看到变量已经加载,所有的对象都在那里

我唯一解决问题的方法是使用以下代码行:

// Load up the index
app.loadIndex();

setTimeout( function(){

    alert(hashtag_index);

}, 500);
通过让javascript休眠500毫秒,我已经给了它足够的时间来加载

当然,我尝试过以下解决方案,但没有一个成功:

解决方案1:进入无限循环

// Load up the index
app.loadIndex();

while(!hashtag_index.length > 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(hashtag_index.length == 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(typeof hashtag_index === 'undefined') {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();


while(typeof hashtag_index == null) {

    continue;

};

alert(hashtag_index);
解决方案2:进入无限循环

// Load up the index
app.loadIndex();

while(!hashtag_index.length > 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(hashtag_index.length == 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(typeof hashtag_index === 'undefined') {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();


while(typeof hashtag_index == null) {

    continue;

};

alert(hashtag_index);
解决方案3:进入无限循环

// Load up the index
app.loadIndex();

while(!hashtag_index.length > 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(hashtag_index.length == 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(typeof hashtag_index === 'undefined') {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();


while(typeof hashtag_index == null) {

    continue;

};

alert(hashtag_index);
解决方案4:*进入无限循环

// Load up the index
app.loadIndex();

while(!hashtag_index.length > 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(hashtag_index.length == 0) {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();

while(typeof hashtag_index === 'undefined') {

    continue;

};

alert(hashtag_index);
// Load up the index
app.loadIndex();


while(typeof hashtag_index == null) {

    continue;

};

alert(hashtag_index);
现在我的问题是,我如何才能在不造成任何未来问题的情况下明确地解决这个问题。我已经尝试用以前的解决方案来解决这个问题,但没有一个成功。

你的app.loadIndex;方法使用异步的ajax。因此,alerthashtag_索引;在ajax调用完成并处理结果之前执行

将代码更改为使用回调,或者我的首选项为app.loadIndex;返回承诺对象

以下是您如何实现承诺:

var loadIndex = function() {
    var def = $.Deferred();

    $.ajax({
        //...
        success: function(result) {
            // do whatever
            def.resolve();
        }
        //...
    });   

    return def.promise();
};

根据您的使用情况,您可能需要扩展此功能以使init返回承诺或接受完成的回调。

您的app.loadIndex;方法使用异步的ajax。因此,alerthashtag_索引;在ajax调用完成并处理结果之前执行

将代码更改为使用回调,或者我的首选项为app.loadIndex;返回承诺对象

以下是您如何实现承诺:

var loadIndex = function() {
    var def = $.Deferred();

    $.ajax({
        //...
        success: function(result) {
            // do whatever
            def.resolve();
        }
        //...
    });   

    return def.promise();
};


根据您的使用情况,您可能需要对此进行扩展,以使init返回承诺或接受完成的回调。

您的应用程序构造函数是什么样子的?威尔!hashtag_index.length>0工作!!您从服务器请求的数据很可能是您通过ajax请求请求的。这就是为什么如果在500毫秒内收到数据,使用setTimeout时会看到数据。循环不会工作,因为js是单线程的,如果是异步的,则在当前脚本完成后执行请求。是的,我使用AJAX请求数据。目前我正在尝试使用@jason-p方法来解决它。@sebastien我只定义了几个变量,其中哪一个是hashtag_indexWhat is you App constructor look?will!hashtag_index.length>0工作!!您从服务器请求的数据很可能是您通过ajax请求请求的。这就是为什么如果在500毫秒内收到数据,使用setTimeout时会看到数据。循环不会工作,因为js是单线程的,如果是异步的,则在当前脚本完成后执行请求。是的,我使用AJAX请求数据。目前我正在尝试使用@jason-p方法来解决它。@sebastien我只定义了几个变量,其中哪一个是hashtag_indexy?关于我使用ajax请求的说法是正确的。但是我已经实现了您描述的回调函数,并且变量仍然没有定义。我做错什么了吗?不管怎样,谢谢你的回答。尽管你的解决方案没有解决问题,但你为我指明了解决问题的正确方向。从现在起,我基本上调用了回调函数中的所有函数。你说的我使用ajax请求是对的。但是我已经实现了您描述的回调函数,并且变量仍然没有定义。我做错什么了吗?不管怎样,谢谢你的回答。尽管你的解决方案没有解决问题,但你为我指明了解决问题的正确方向。从现在起,我基本上调用了回调函数中的所有函数。