【发布时间】:2009-12-28 03:56:43
【问题描述】:
当用户输入内容时,我需要使用 AJAX 对照数据库中的相应数据检查用户输入,但我无法根据数据库中的数据连续检查用户输入,因为这会使我的数据库服务器不堪重负。我只想在用户输入期间以显着/明显/合理的暂停开始 AJAX 请求。例如,输入光标一两秒不动。从用户的角度来看,他认为检查是实时的。如何使用 Jquery 做到这一点?
为什么我的代码没有按预期工作?
function subjectivecheck(id){
alert(id);
var cost=(new Date().getTime() - start.getTime())/1000;
var value=$('#question'+id).val();
$.post("subjectivecheck.php?",{val:value, qid:id,time:cost, a_id:"<?php echo $announcementid; ?>"},function(xm){
switch(parseInt(xm)){
case 4:
{ $htm='Congrats,you have passed the test.';
$('#success').css({"color":"green"});
$('#success').text($htm);
return;
}
case 1:
{
$htm='V';
$('#sign'+id).css({"color":"green"});
$('#sign'+id).text($htm);
break;
}
case 0:{
$htm='X';
$('#sign'+id).css({"color":"red"});
$('#sign'+id).text($htm);
break;
}
case 3:{
$('#subjectivequestion').text('You have failed at this announcement.');
$('#choicequestions').text(" ");
}
}
});
}
var ajaxCallTimeoutID = null;
function subjectivecheckcallback(id){
if (ajaxCallTimeoutID != null)
clearTimeout(ajaxCallTimeoutID);
ajaxCallTimeoutID = setTimeout(subjectivecheck(id), 1000);
}
【问题讨论】:
标签: php javascript jquery ajax