简体   繁体   English

ajax调用持续运行

[英]ajax call is continuously running

I'm using ajax to check whether there is no new alerts stored in my sql database. 我正在使用ajax检查我的sql数据库中是否没有存储新警报。 I've set it to run every 30 seconds but instead it keeps on calling the function that checks the database every second. 我将其设置为每30秒运行一次,但它会继续调用每秒检查一次数据库的函数。 Here is my code. 这是我的代码。

This is code in the view. 这是视图中的代码。 "_Tasks" is my partial view and it will be the view that is going to be updated. “ _Tasks”是我的局部视图,它将是将要更新的视图。 I haven't done anything for a success return because it never reaches that code. 我没有为成功返回做任何事情,因为它永远不会到达该代码。

  @Html.Action("_Tasks")
           <script type="text/javascript">
                (function poll() {

                    $.ajax({                       
                        type: 'GET',
                        cache: false,
                        url: '@Url.Action("TasksRefresh")',
                        dataType: "json", 
                        complete: poll, 
                        timeout: 30000,
                        success: function (data) {
                            if (data.success == true)
                                alert("Hello");

                        } 
                    });
                })();
        </script>

This is in the controller 这是在控制器中

public JsonResult TasksRefresh()
        {
            var alerts = getAlerts();
            var cache = HttpRuntime.Cache["Tasks"];
            if ( cache == alerts)
            {
                return Json(new
                                {
                                    success = false,
                                });
            }
            else
            {
                return Json(new
                {
                    success = true,
                   // View = this.Re
                });
            }

        }

Try this: 尝试这个:

(function poll() {
  $.ajax({                       
    type: 'GET',
    cache: false,
    url: '@Url.Action("TasksRefresh")',
    dataType: "json", 
    complete: function () { setTimeout(poll, 30000); }, // Changed, call poll again when 30s has pasted
    timeout: 30000, // This just says it fails if the request takes more than 30s
    success: function (data) {
      if (data.success == true)
        alert("Hello");

    } 
  });
})();

Timeout is not a delay. 超时不是延迟。 Timeout is how long it should wait before considering the request a failure. 超时是在考虑请求失败之前应等待的时间。 By setting complete to poll , you are telling it to immediately make another call. 通过将complete设置为poll ,您告诉它立即拨打另一个电话。 You need to put a delay in your complete function. 您需要延迟complete功能。

What I can guess is, you are executing the poll function again on complete of the ajax call. 我能猜到的是,您将在ajax调用完成后再次执行poll函数。 It might just take a second to complete that ajax call. 完成该ajax调用可能只需要一秒钟。
You can use the timer at javascript code. 您可以在JavaScript代码中使用计时器。 try using settimeout or setInterval . 尝试使用settimeoutsetInterval And do ajax call in that timer. 并在该计时器中执行ajax调用。 See if is helpful. 看看是否有帮助。

timeout: 30000 is just to make sure that your ajax call would wait for response for maximum of that much time. timeout: 30000只是为了确保您的ajax调用将等待响应的时间最长。

You are executing your poll function as soon as ajax call completes that is why calls are made so quick. 一旦ajax调用完成,您将执行轮询功能,这就是为什么调用如此之快的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM