我编写了一个应用程序来向用户询问调查类型应用程序中的问题。 当用户点击答案时,AJAX(w/jQuery)会调用PHP来记录答案,然后显示下一个问题。这一切都很好。 问题是,我想在点击答案后向用户显示一些统计信息两秒钟。为此,我调用一个函数来显示统计信息div,然后等待两秒钟,隐藏该div然后显示新问题。想要让AJAX暂停,显示进度条2秒,然后继续
问题是,当我使用.wnen,然后jQuery函数屏幕'冻结',所以我不能使用动画gif让用户知道下一个问题即将到来。 GIF随着屏幕的其余部分一起冻结。
下面是我用来显示下一个问题的功能。
有没有更好的方法来暂停我在做什么?
的网址是:www.imazy.com/askQuestions.php
了以下功能被称为像这样:
$.ajax({
type: "POST",
url: "recordUserAns.php",
data: data,
cache: false,
async: false,
success: function(html){
showNextQn(html);
}
});
function showNextQn($response){
// -------------- Pause and fade in/out -------------- //
var btnVal = document.getElementById("toggleStats").innerHTML;
if(btnVal == "Show Stats"){ // Change the text
var showStats = 0;
}else{
var showStats = 1;
}
function showDiv(){
var dfd = $.Deferred();
if (showStats){
$('.ansPercent').fadeIn(fadeInTime, dfd.resolve); // Show the hidden div
// $('span#progBar').fadeIn(fadeInTime, dfd.resolve); // Show the progress bar
}else{
$('#content').fadeOut(fadeOutTime, dfd.resolve);
}
return dfd.promise();
}
$.when(showDiv())
.then(function(ajaxResult){
if (showStats){
pauseScript(qnPauseTime); // Pause given # milliseconds
}
// $('span#progBar').fadeOut(0); // Hide the progress bar
$('#content').html($response); // Return the NEXT question
$('#content').fadeIn(fadeInTime);
// 'ajaxResult' is the server's response
});
document.getElementById("prevBtn").style.visibility="Visible";
// Remove every instance of the class, as it is no longer needed
$('a.ansHolder').removeClass('selected');
}
'async:false'会锁定浏览器,所以在使用时请小心。 –