2016-11-22 21 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參數。

相關問題