如何在命令之间的Ajax中花点时间

时间:2019-07-05 08:17:59

标签: javascript ajax

我想在重定向到url之前添加几秒钟

$(document).on("click", ".post-dataDraft", function() {
    $.ajax({
        method: "POST",
        url: "<?php echo base_url('Lap_geologi/postAll'); ?>",
    })
    .done(function(data) {
        $('#konfirmasiPost').modal('hide');
        $('.msg').html(data);
        effect_msg();
        //I need a moment here
        window.location='<?php echo base_url('Lap_geologi'); ?>';
    })
    .error(function(data) {
        console.log(data);
    })
})

如何在标签中添加一些内容//

2 个答案:

答案 0 :(得分:2)

您可以为此使用setTimeout

setTimeout(() => {
  // your code here
  window.location='<?php echo base_url('Lap_geologi'); ?>';
}, 1000);

1000是超时(以毫秒为单位),因此在这种情况下,回调将在1秒后被调用。

答案 1 :(得分:1)

您可以按以下方式使用“ setTimeout”:

$(document).on("click", ".post-dataDraft", function() {
$.ajax({
    method: "POST",
    url: "<?php echo base_url('Lap_geologi/postAll'); ?>",
})
.done(function(data) {
    $('#konfirmasiPost').modal('hide');
    $('.msg').html(data);
    effect_msg();
    //I need a moment here
    setTimeout(function() {
      //redirect here
      window.location='<?php echo base_url('Lap_geologi'); ?>';
    }, 5000);

})
.error(function(data) {
    console.log(data);
})

})