【发布时间】:2018-11-20 09:16:25
【问题描述】:
我正在为儿童制作一个小游戏。游戏已完成构建,但面临的问题是在需要时显示警报框。有时显示有时不显示。当我按下检查元素时,我发现它打印在控制台上但没有出现在警报框中。有时当我硬重置浏览器时,它会在需要时显示警告框。我清除了浏览器缓存并有时尝试其工作,有时不尝试。但它会根据我的代码正确打印在控制台上。
请有人帮助我解决这个问题。我从来没有遇到过这种问题,很奇怪,很紧张。
下面是我的jquery代码:
<script>
$(document).ready(function(){
$("#feed_btn").hide();
$("#finish_btn").hide();
$("#start_btn").click(function(){
$("#start_btn").hide();
$("#feed_btn").show();
});
});
function RestartGame()
{
location.reload();
}
var i=0;
function PerformGame()
{
i++;
$.ajax({
url: 'perform_game.php',
type: "POST",
data: {'button_clicked': i},
dataType: 'json',
success: function(data) {
if(data == "Maximum Clicks Reached. You Lose !") {
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
if(data == "Maximum Clicks Reached. You Won !") {
swal({title: "Wow !",text: data,type: "success"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
if(data == "Hard Luck ! One or Some animal(s) died.") {
swal({title: "Dead !",text: data,type: "warning"});
}
if(data == "Farmer Died ! You Lose !"){
swal({title: "Game Over !", text: data, type: "error"});
$("#feed_btn").hide();
$("#finish_btn").show();
}
}
});
}
</script>
下面是我的php代码:
foreach($_SESSION['animal_life'] as $key => $value)
{
if ($value == '0') {
if ($key == 'Farmer'){
$msg = "Farmer Died ! You Lose !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
break;
}
$search_animal = array_search($key, $_SESSION['animals']);
unset($_SESSION['animals'][$search_animal]);
}
$check_animals_left = count($_SESSION['animals']);
if($check_animals_left < $count_total_animals) {
$msg = "Hard Luck ! One or Some animal(s) died.";
}
}
if ($no_of_times_button_clicked == '50') {
$counter = 0;
if (in_array('Farmer', $_SESSION['animals'])) {
$counter++;
}
if (in_array('Cow1', $_SESSION['animals']) || in_array('Cow2', $_SESSION['animals'])) {
$counter++;
}
if (in_array('Bunny1', $_SESSION['animals']) || in_array('Bunny2', $_SESSION['animals']) || in_array('Bunny3', $_SESSION['animals']) || in_array('Bunny4', $_SESSION['animals']))
{
$counter++;
}
if ($counter == 3)
{
$msg = "Maximum Clicks Reached. You Won !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
} else {
$msg = "Maximum Clicks Reached. You Lose !";
unset($_SESSION['animals']);
unset($_SESSION['animal_life']);
unset($_SESSION['final_result']);
}
}
if ($msg) {
echo json_encode($msg);
}
}
【问题讨论】:
-
仅用于测试目的 - 将 sweetalert (swal) 替换为原生 js alert 并查看错误是否仍然存在。
-
@MrAleister 是的,常规警报框也发生了同样的情况。我努力刷新了昨天工作的浏览器,今天又显示了问题。这次我清除了缓存并硬刷新了浏览器,但问题仍然存在。我在 ajax 中使用了 async:False 但对我来说没有成功。
-
可以显示ajax请求的响应吗?
-
另一件事是 - 您的 if 语句依赖于长/复杂的字符串比较,因此容易出现拼写错误。尝试简化响应和条件(使用“ALL_OK”、“ANIMAL_DEAD”等代码),并仅在警报代码中保留长字符串。
-
@BanujanBalendrakumar 让我尝试更改 MrAleistr 所说的长句...如果问题仍然存在,那么我将分享我的 Anydesk 或 TeamViewer 的 ID 和密码!
标签: javascript php jquery arrays ajax