Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery中设置ajax调用超时_Javascript_Jquery_Ajax_Function - Fatal编程技术网

Javascript 在jquery中设置ajax调用超时

Javascript 在jquery中设置ajax调用超时,javascript,jquery,ajax,function,Javascript,Jquery,Ajax,Function,有没有一种方法可以在这个ajax调用中使用setTimeout。这是我的代码: jQuery.ajax({ type : "POST", url : dir+"all/money/myFile.php", data : "page="+data.replace(/\&/g, '^'), success : function(msg) { var info = jQuery('.product_overview #allinfo'

有没有一种方法可以在这个ajax调用中使用setTimeout。这是我的代码:

jQuery.ajax({
    type    : "POST",
    url     : dir+"all/money/myFile.php",
    data    : "page="+data.replace(/\&/g, '^'),
    success : function(msg) {
    var info = jQuery('.product_overview #allinfo').html();
    var url = '<a href="http://www.mySite.com/openMe/letmeview.php?preview='+msg+'.html" target="_blank" class="check_link_preview" rel="'+msg+'.html">Check Preview</a>';          jQuery('.option_additional').next().find('textarea:first').text(info+url);
                },
    complete: function() {
    jQuery('.add-to-cart button.btn-cart').delay(500).trigger('click');
}
});
jQuery.ajax({
类型:“POST”,
url:dir+“all/money/myFile.php”,
数据:“page=“+data.replace(/\&/g,”^'),
成功:功能(msg){
var info=jQuery('.product_overview#allinfo').html();
var url='';jQuery('.option_additional').next().find('textarea:first').text(info+url);
},
完成:函数(){
jQuery('.add to cart button.btn cart')。延迟(500)。触发器('click');
}
});
我想在触发这个ajax之前做些什么,这就是为什么我要使用setTimeout或者延迟这个操作的原因

我该怎么做


提前感谢:)

您可以在发送之前使用

$(function() {

    function callBeforeAjax() {
        alert('and now do ajax');
    }

    $.ajax({
        beforeSend: callBeforeAjax,
        type: "POST",
        url: "/",
        data: "",
        success: function(msg) {},
        complete: function(msg) {
            alert(msg);
        }
    });
});

请参阅,您可以在发送前使用

$(function() {

    function callBeforeAjax() {
        alert('and now do ajax');
    }

    $.ajax({
        beforeSend: callBeforeAjax,
        type: "POST",
        url: "/",
        data: "",
        success: function(msg) {},
        complete: function(msg) {
            alert(msg);
        }
    });
});

请参阅之前未将jquery与setTimeout一起使用,但请重试

var t = window.setTimeout(function, delay);

将上述代码中的函数替换为jquery函数。

之前没有将jquery与setTimeout一起使用过,但请尝试

var t = window.setTimeout(function, delay);

用jquery函数替换上述代码中的函数。

@Tols是正确的,它可以工作。尝试以下操作:
setTimeout(函数(){jQuery('.addtocart button.btn cart')。触发器('click');},500)

@Tols是对的,它可以工作。尝试以下操作:
setTimeout(函数(){jQuery('.addtocart button.btn cart')。触发器('click');},500)

complete: function () {
  setTimeout(function () {
     // your action here
  }, 500);
}

在完整功能中:

complete: function () {
  setTimeout(function () {
     // your action here
  }, 500);
}

所以如果你想用它-为什么不直接用它呢?!;-)@zerkms:我如何将它放入setTimeout函数中?你能给我一个例子吗?请使用我上面的代码。谢谢:)嗯,你用过setTimeout吗?有什么原因吗?“行不通”不是一个问题的解释。所以如果你想用它-为什么不直接用它呢?!;-)@zerkms:我如何将它放入setTimeout函数中?你能给我一个例子吗?请使用我上面的代码。谢谢:)嗯,你用过setTimeout吗?有什么原因吗?“不起作用”不是问题的解释。嗯,找不到
delay
属性。您从何处获取它?它应该是
beforeSend:callBeforeAjax
,而不是
beforeSend:callBeforeAjax()
您不需要延迟属性,因为beforeSend将在Ajax之前被调用。我已经更新了我的答案,没有延迟方法,但是我现在就添加引用。哈哈,你必须先把引用放在那个插件上。附:我不是OP;-):)是的,可能是不得不嘿!LoLUhm,找不到
delay
属性。您从何处获取它?它应该是
beforeSend:callBeforeAjax
,而不是
beforeSend:callBeforeAjax()
您不需要延迟属性,因为beforeSend将在Ajax之前被调用。我已经更新了我的答案,没有延迟方法,但是我现在就添加引用。哈哈,你必须先把引用放在那个插件上。附:我不是OP;-):)是的,可能是不得不嘿!英雄联盟