Javascript数组未定义?

Javascript数组未定义?,javascript,ajax,Javascript,Ajax,这似乎是一个可笑的琐碎问题,但我无法解决它 我有这个功能var q被分配给字符串数组警报(q)在调用函数时成功打印整个数组 function initQuestions() { $.post("quiz.php", { 'func': 'load' }, function(data) { var q = data.split(".\n"); alert(q); return q; }); } 但是,当我尝试使用函数时(如下2种方式

这似乎是一个可笑的琐碎问题,但我无法解决它

我有这个功能
var q
被分配给字符串数组<代码>警报(q)在调用函数时成功打印整个数组

function initQuestions() {
    $.post("quiz.php", { 'func': 'load' }, function(data) {
        var q = data.split(".\n");
        alert(q);
        return q;
    });
}
但是,当我尝试使用函数时(如下2种方式所示),我被告知数组未定义。为什么会这样

var questions;

$(function() {
    //This doesn't work
    questions = initQuestions();
    alert(questions);

    //Neither does this
    alert(initQuestions());
});

经过进一步研究,我向
initQuestions()
添加了一个回调,但得到了相同的结果。

post调用是异步的:

基本上,initQuestions()函数在
post
完成之前返回
null

这样想:

function initQuestions() {
    $.post("quiz.php", { 'func': 'load' }, function(data) {
        var q = data.split(".\n");
        alert(q);
        return q; // <- returns to the caller of function(data), not initQuestions()!
    });
    return null; // <- This happens before function(data) is complete
}
函数initQuestions(){
$.post(“quick.php”,{'func':'load'},函数(数据){
var q=数据分割(“.\n”);
警报(q);

返回q;//post调用是异步的:

基本上,initQuestions()函数在
post
完成之前返回
null

这样想:

function initQuestions() {
    $.post("quiz.php", { 'func': 'load' }, function(data) {
        var q = data.split(".\n");
        alert(q);
        return q; // <- returns to the caller of function(data), not initQuestions()!
    });
    return null; // <- This happens before function(data) is complete
}
函数initQuestions(){
$.post(“quick.php”,{'func':'load'},函数(数据){
var q=数据分割(“.\n”);
警报(q);

return q;//post是异步的,return不会返回到您期望的函数,而是返回到成功处理程序:

function initQuestions() {
  $.post("quiz.php", { 'func': 'load' }, function(data) { // <- this function has that return
    var q = data.split(".\n");
    alert(q);
    return q;
  });
}

结果是一样的,但由于异步请求,如果您有potatoe internet连接,您的JS代码不会冻结几秒钟。

帖子是异步的,返回的不是您期望的函数,而是成功处理程序:

function initQuestions() {
  $.post("quiz.php", { 'func': 'load' }, function(data) { // <- this function has that return
    var q = data.split(".\n");
    alert(q);
    return q;
  });
}
var questions; // undefined right now
initQuestions(function(_questions){
  questions = _questions; // the value has been set thanks to the anonymous function you passed as the callback
})

function initQuestions(callback  // this is a callback function reference) {

  // This HTTP Post call takes time to complete and is done asynchronously
  $.post("quiz.php", { 'func': 'load' }, function(data) {
    var q = data.split(".\n");
    // Luckily your initQuestions now takes in a callback function
    // You have access to the callback function here and when ready you can use it
    callback(q);
  });
  // JavaScript engine doesn't wait for your HTTP call to complete 
  // and will continue to execute the function
  // In this case, your function isn't returning anything hence undefined

}


结果是一样的,但由于异步请求,如果您连接了potatoe internet,您的JS代码不会冻结几秒钟。

不要尝试提醒它,而是使用
console.log()
检查浏览器中的错误日志。这是有原因的。请使用console.log而不是alert。欢迎来到奇妙的异步web编程世界。您正在读取var q设置之前的值。从外观上看,这是一个范围问题。不要尝试对其发出警报,而是使用
console.log()
检查浏览器中的错误日志。这是有原因的。请使用console.log而不是alert。欢迎来到奇妙的异步web编程世界您正在读取var q设置之前的值。从外观上看,这是一个范围问题可能更好示例(事实上重复)可能更好示例(事实上重复)asynch=false,这就解决了这个问题,这个解决方案是不必要的复杂化这表明asynch=false不应该被@kajacx使用-async false对于用户体验和糟糕的设计决策来说是不好的(它在ajax调用期间锁定了浏览器UI)是的,这种锁定是不好的,但是OP问他的initQuestions()函数如何从ajax返回数据,而这不能通过asynch=false实现“为什么会这样?”是问题。asynch=false,解决了这个问题,这个解决方案是不必要的复杂这表明asynch=false不应该被@kajacx使用-async false对于用户体验和糟糕的设计决策来说是不好的(它在ajax调用期间锁定了浏览器UI)是的,这种锁定是不好的,但是OP问他的initQuestions()函数如何从ajax返回数据,而这不能通过asynch=false实现“为什么会这样?”这就是问题所在。这解释了为什么它不起作用,但不是OP应该做什么。谢谢,这起作用了,但这是我以前尝试过的没有用的东西。这一次起作用的原因是将函数调用放在文档的最顶端。准备好了。为什么一段代码放在一个不相关的饼之后就不起作用了其他代码的ce?实际上我说得太快了。
问题
在回调之外仍然没有定义。如果在回调启动之前在回调之外调用代码,则会出现这种情况。如果要使用questions变量,则必须在加载问题之后使用它。这解释了为什么它不起作用,但操作说明了什么uld可以这样做。谢谢,这很有效,但我以前尝试过,但没有成功。这次成功的原因是将函数调用放在文档的最顶端。准备好了。为什么一段代码放在一段不相关的代码之后就不起作用了?实际上我说得太快了。
问题仍然存在在回调外部未定义如果您在调用回调之前调用回调外部的代码,则为。如果要使用questions变量,则必须在加载问题后使用它
var questions; // undefined right now
initQuestions(function(_questions){
  questions = _questions; // the value has been set thanks to the anonymous function you passed as the callback
})

function initQuestions(callback  // this is a callback function reference) {

  // This HTTP Post call takes time to complete and is done asynchronously
  $.post("quiz.php", { 'func': 'load' }, function(data) {
    var q = data.split(".\n");
    // Luckily your initQuestions now takes in a callback function
    // You have access to the callback function here and when ready you can use it
    callback(q);
  });
  // JavaScript engine doesn't wait for your HTTP call to complete 
  // and will continue to execute the function
  // In this case, your function isn't returning anything hence undefined

}