在该beforeSend
部分中,我添加了一个setTimeout
. 如果 ajax 花费超过 20 秒,则会出现超时消息。如果 ajax 在 20 秒前达到成功,setTimeout
它仍在运行并出现超时消息。
如果 ajax 在不到 20 秒内完成,如何防止出现超时消息?
形式
<?= $form->field($relasi, 'npsn')->textInput(['class' => 'form-control', 'id' => 'npsn', 'onkeyup' => 'sendFirstCategory(this.value)', 'autocomplete' => 'off'])->label(false); ?>
JavaScript
function sendFirstCategory(npsn)
{
var test = npsn;
var timeoutvar = null;
$.ajax({
url: '<?php echo \Yii::$app->getUrlManager()->createUrl('keranjang/isi-data-ajax') ?>',
type: 'POST',
data: { test: test },
beforeSend:function()
{
$('#hokya').show();
$('#hokya').html('<img src="../img/Preloader_1.gif" alt="Loading..." />');
var a = "";
$('form [id=nm_sekolah]').val(a);
$('form [id=email]').val(a);
$('form [id=telp]').val(a);
$('form [id=alamat]').val(a);
$('form [id=penanggung_jawab]').val(a);
timeoutvar = setTimeout(function() {
$("#hokya").hide();
$('#login-modal').modal('show');
$('form [id=nm_sekolah]').val(a);
$('form [id=email]').val(a);
$('form [id=telp]').val(a);
$('form [id=alamat]').val(a);
$('form [id=penanggung_jawab]').val(a);
}, 20000);
},
success: function(data)
{
clearTimeout(timeoutvar);
var o = $.parseJSON(data);
$.each(o, function(key, value){
$('form [id=' + key + ']').val(value);
});
$('#hokya').hide();
}
});
}