Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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 设置ajax调用中的延迟不起作用_Javascript_Jquery_Ajax_Delay - Fatal编程技术网

Javascript 设置ajax调用中的延迟不起作用

Javascript 设置ajax调用中的延迟不起作用,javascript,jquery,ajax,delay,Javascript,Jquery,Ajax,Delay,在将success HTML设置为div之前,我需要5秒的延迟。我在下面尝试过,但没有成功。有人有想法吗 $("#glyphicon-chevron-left-daily").click(function () { var endDate = $("#DailyViewButtonOk1").data("date"); var previousButtonHtml = $(this).html(); $(this).html("<span class=\"glyph

在将success HTML设置为div之前,我需要5秒的延迟。我在下面尝试过,但没有成功。有人有想法吗

$("#glyphicon-chevron-left-daily").click(function () {
    var endDate = $("#DailyViewButtonOk1").data("date");
    var previousButtonHtml = $(this).html();
    $(this).html("<span class=\"glyphicon glyphicon-refresh\"></span>");

    $(function () {
        callAjax();
    });

    function callAjax() {
        $.ajax({
            url: loadUrl,
            type: 'POST',
            load: "<img src='img/load.gif' alt='loading...' />",
            data: { startDate: startDate, endDate: endDate },

            success: function (response) {
                $("#_DailyViewResults").html(response);
                $("#_DailyViewResults").show();
                setTimeout(callAjax, 5000);
            },
            error: function () {

            }
        });
    }
    $(this).html(previousButtonHtml);
});
$(“#字形图标雪佛龙左每日”)。单击(函数(){
var endDate=$(“#DailyViewButtonOk1”)。数据(“日期”);
var previousButtonHtml=$(this.html();
$(this.html(“”);
$(函数(){
callAjax();
});
函数callAjax(){
$.ajax({
url:loadUrl,
键入:“POST”,
负载:“,
数据:{startDate:startDate,endDate:endDate},
成功:功能(响应){
$(“#_DailyViewResults”).html(响应);
$(“#_DailyViewResults”).show();
setTimeout(callAjax,5000);
},
错误:函数(){
}
});
}
$(this.html(previousButtonHtml);
});
下面的代码现在可以工作了。现在的问题是按钮内的原始跨度没有恢复

$("#glyphicon-chevron-left-daily").click(function () {
    var endDate = $("#DailyViewButtonOk1").data("date");
    var previousButtonHtml = $(this).html();
    $(this).html("<span class=\"glyphicon glyphicon-refresh\"></span>");
    $.ajax({
        url: loadUrl,
        type: 'POST',
        data: { startDate: startDate, endDate: endDate },

        success: function (response) {
            setTimeout(function (response) {
                $("#_DailyViewResults").html(response);
                $("#_DailyViewResults").show();
                $(this).html(previousButtonHtml);
            }, 1000, response);
        },
        error: function () {

        }
    });
});
$(“#字形图标雪佛龙左每日”)。单击(函数(){
var endDate=$(“#DailyViewButtonOk1”)。数据(“日期”);
var previousButtonHtml=$(this.html();
$(this.html(“”);
$.ajax({
url:loadUrl,
键入:“POST”,
数据:{startDate:startDate,endDate:endDate},
成功:功能(响应){
设置超时(功能(响应){
$(“#_DailyViewResults”).html(响应);
$(“#_DailyViewResults”).show();
$(this.html(previousButtonHtml);
},1000,回复);
},
错误:函数(){
}
});
});
$(“#字形图标-雪佛龙-每日左”)。单击(功能(){
var endDate=$(“#DailyViewButtonOk1”)。数据(“日期”);
var previousButtonHtml=$(this.html();
$(this.html(“”);
var=这个;
$.ajax({
url:loadUrl,
键入:“POST”,
负载:“,
数据:{startDate:startDate,endDate:endDate},
成功:功能(响应){
var参数=[];
参数['response']=响应;
参数['previousButtonHtml']=previousButtonHtml;
params['that']=that;
设置超时(函数(参数){
$(“#_DailyViewResults”).html(params['response');
$(“#_DailyViewResults”).show();
$(params['that']).html(params['previousButtonHtml']);
},5000,params);
},
错误:函数(){
}
});
});

我认为情况是ajax,它正在执行
success
函数,在它的上下文中没有callAjax,因此
callAjax
被视为
未定义的

success: function (response) {
                    $("#_DailyViewResults").html(response);
                    $("#_DailyViewResults").show();
                    console.log('callAjax=', callAjax);
                    setTimeout(callAjax, 5000);
                },
您可以通过从ajax的回调中登录到控制台
callAjax
的值来轻松检查

解决方案是在上下文中保留callAjax函数,如下所示:

//in ajax call's properties object
success: (function(callAjax) { return function (response) {
    $("#_DailyViewResults").html(response);
    $("#_DailyViewResults").show();
    setTimeout(callAjax, 5000);
}})(callAjax),
error: //...

请澄清你的问题。如果目的是显示消息,为什么要“setTimeout(callAjax,5000);”调用相同的方法imside ajax success?没有指定loadUrl。忽略loadUrl。我现在意识到它甚至都不有效。不一定没有指定
loadUrl
,例如,可以将它定义为一个全局函数,但$(this).html(previousButtonHtml)会立即执行。我在ajax post之前将其设置为另一个图标,并在post之后将其设置为原始图标。将其放入settimeout函数中这样做根本不会将图标替换为原始图标。按钮代码是@sanjaysharma?还有其他问题吗?是的…请查看我的编辑…所以下面的代码现在可以工作了。现在的问题是,按钮内的原始跨距没有恢复。这是对未更换原始按钮跨距的响应吗?因为延迟现在起作用了,但是上面没有你说的“上面”是什么意思?第一个代码段(带有
console.log
)显然不起作用
//in ajax call's properties object
success: (function(callAjax) { return function (response) {
    $("#_DailyViewResults").html(response);
    $("#_DailyViewResults").show();
    setTimeout(callAjax, 5000);
}})(callAjax),
error: //...