Javascript 重复调用命中php文件的函数
我有一个php文件,echo是一个json编码的单词。我有一个javascript函数来检索这个单词。我想调用这个函数,检查返回的单词是否已经在数组中,如果已经在数组中,我想调用另一个单词。我基本上想重复这个过程,直到我得到一个数组中没有的单词。我正在努力解决这个问题,所以我需要一些关于如何做到这一点的建议。我只想在客户端做这件事。目前,当我调用getWord()时,它返回undefined,但函数本身可以工作,所以我怀疑当时还没有检索到这个单词Javascript 重复调用命中php文件的函数,javascript,php,jquery,Javascript,Php,Jquery,我有一个php文件,echo是一个json编码的单词。我有一个javascript函数来检索这个单词。我想调用这个函数,检查返回的单词是否已经在数组中,如果已经在数组中,我想调用另一个单词。我基本上想重复这个过程,直到我得到一个数组中没有的单词。我正在努力解决这个问题,所以我需要一些关于如何做到这一点的建议。我只想在客户端做这件事。目前,当我调用getWord()时,它返回undefined,但函数本身可以工作,所以我怀疑当时还没有检索到这个单词 <?php $sql = "SELECT
<?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);
});
});
函数getWord(){
变异词;
$(文档).ready(函数(){
$.getJSON(“getWord.php”,函数(数据){
字=数据;
警报(“第一警报:+字”);
});
});
返回词;
}
$(文档).ready(函数(){
$(“#新轮”)。在(“单击”,函数(){
currentWord=getWord();
警报(“第二警报:+currentWord”);
//检查这里的数据是否已经在WordsFar arary中,如果已经在WordsFar arary中,则从getword.php获取另一个单词
document.getElementById(“input1”).style.visibility='visible';
//currentWord=data;//设置当前工时
寿命=6;//重置寿命
跟踪器=0;
不正确的文字猜测=”;
allGuessedLetters=“”;
updateleters();
document.getElementById('hangman')。innerHTML='';
createTable(currentWord);
output.innerHTML=''+messages.validLetter+'';
警报(currentWord);
});
});
我想,问题是在getWord
函数中return
在服务器应答和word
分配发生之前执行。尝试将$.ajax
与async:false
参数一起使用。问题在于对php服务器的ajax调用在程序的其余部分完成运行后得到解决。此时,getWord()
函数的工作方式如下:
word
。此时word
等于undefined
word
,此时仍然等于undefined
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(){
返回$.getJSON(“getWord.php”);
}
$(文档).ready(函数(){
$(“#新轮”)。在(“单击”,函数(){
getWord().done(函数(currentWord){
警报(“当前单词为”+当前单词);
//检查这里的数据是否已经在WordsFar arary中,如果已经在WordsFar arary中,则从getword.php获取另一个单词
document.getElementById(“input1”).style.visibility='visible';
//currentWord=data;//设置当前工时
寿命=6;//重置寿命
跟踪器=0;
不正确的文字猜测=”;
allGuessedLetters=“”;
updateleters();
document.getElementById('hangman')。innerHTML='';
createTable(currentWord);
output.innerHTML=''+messages.validLetter+'';
警报(currentWord);
});
});
});
除此之外,在
getWord
函数中不需要使用$(document).ready
。只有在加载文档时才会调用该函数。getWord.php的源代码在哪里?顺便说一句,$.getJson()
是一种方法。因此,您的getWord()
-函数将始终返回空值。它只是一个单词数据库。是否有一种方法仅在getWord()完成后调用该方法来执行代码?它是否会提醒word?如果有,那么请检查我在第一条评论中发布的关于异步的链接。您不需要$(文档)。准备好了
内置getWord函数!此外,它完全破坏了您的逻辑。因此,如果我将getWord().done{…}中的所有内容放在while循环中,我应该能够一直获取一个单词,直到满足条件为止?在while循环中执行异步调用将不起作用,因为您不能等待异步调用的结果,然后再继续下一次写入。实现所需操作的最简单方法是将wordsFar
数组发送到php文件,以便服务器可以返回数组中不包含的单词。这样,您只需在javascript中执行一个异步调用