2016-11-22 16 views
1

我有一个php文件,它的echo是一个json编码的词。我有一个JavaScript函数来检索这个词。我想调用这个函数,检查返回的单词是否已经在一个数组中,如果是,我想调用另一个单词。我基本上想重复这个过程,直到我得到一个不在我的数组中的单词。我正在努力,所以我需要一些建议如何做到这一点。我只想在客户端做到这一点。目前,当我调用getWord()时,它返回undefined,但函数本身起作用,所以我怀疑当时还没有检索到该单词。反复调用一个打php文件的函数

<?php 
$sql = "SELECT * FROM words ORDER BY RAND() LIMIT 1"; 

$result = $db->query($sql); 

$row = $result->fetch_assoc(); 
$word = $row['word']; 
echo json_encode($word); 
?> 


function getWord(){ 
     var word; 
     $(document).ready(function(){ 
      $.getJSON("getWord.php",function(data){ 
       word = data; 
       alert("1st alert: " + word); 
      }); 

     }); 
     return word; 
    } 

$(document).ready(function() { 
      $("#newRound").on("click",function(){ 
        currentWord = getWord(); 
        alert("SEcond alert: " + currentWord); 
        //check here if data is already in wordsSoFar arary and if it is, get another word from getword.php 
        document.getElementById("input1").style.visibility = 'visible'; 
        //currentWord = data; //set the current work 
        lives = 6; //reset lives 
        tracker = 0; 
        incorrectLettersGuessed = ""; 
        allGuessedLetters = ""; 
        updateLetters(); 
        document.getElementById('hangman').innerHTML = '<center><img src="stage1.png"></center>'; 
        createTable(currentWord); 
        output.innerHTML = '<center>'+messages.validLetter + '</center>'; 
        alert(currentWord); 

      }); 
    }); 
+0

getWord.php的来源在哪里?顺便说一句,'$ .getJson()'是一个[异步](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests)方法。你的getWord()函数总是会返回一个空值。 –

+0

它只是一个单词数据库。只有在getWord()被竞争后调用方法之后才有办法执行代码吗? –

+0

它提醒这个词吗?如果是这样,那么请查看我在第一条评论中发布的关于_asynchronous_的链接。 –

回答

3

问题是,在您的PHP服务器的ajax调用在您的程序的其余部分完成运行后得到解决。此时getWord()功能的工作原理如下:

  1. 创建一个变量word。此时word等于undefined
  2. 开始异步调用php服务器。
  3. return word,在这一点上仍然等于undefined
  4. 返回word后,异步调用将被解析并执行回调函数。

而不是从getWord()返回一个单词,而应该返回此异步调用创建的承诺并在主函数中处理结果。就像这样:

function getWord(){ 
    return $.getJSON("getWord.php"); 
} 


$(document).ready(function() { 
     $("#newRound").on("click",function(){ 
       getWord().done(function(currentWord) { 
        alert("The current word is " + currentWord); 
        //check here if data is already in wordsSoFar arary and if it is, get another word from getword.php 
        document.getElementById("input1").style.visibility = 'visible'; 
        //currentWord = data; //set the current work 
        lives = 6; //reset lives 
        tracker = 0; 
        incorrectLettersGuessed = ""; 
        allGuessedLetters = ""; 
        updateLetters(); 
        document.getElementById('hangman').innerHTML = '<center><img src="stage1.png"></center>'; 
        createTable(currentWord); 
        output.innerHTML = '<center>'+messages.validLetter + '</center>'; 
        alert(currentWord); 
       }); 


     }); 
}); 

除了这个问题,这是不必要的getWord功能使用$(document).ready。该函数仅在文档加载时被调用。

+0

中都会破坏你的逻辑,所以如果我把getWord()。done {...}中的所有内容放在while循环中,我应该能够保持一个词,直到满足条件为止。 –

+0

在while循环中执行异步调用将不起作用,因为在继续进行下一次迭代之前,您无法等待异步调用的结果。实现你想要做的最简单的方法是将'wordsSoFar'数组发送给你的PHP文件,这样服务器可以返回一个不包含在数组中的单词。这样你只需要在javascript中进行一次异步调用。 – yadejo

+0

如果你不想修改php,你可以使用由AlexandrX提供的解决方案 – yadejo

0

我想,问题是,在你的getWord功能return执行服务器回答的word分配发生了。尝试使用$.ajaxasync:false参数。

相关问题