Jquery:自动调用函数

时间:2017-11-17 06:39:44

标签: jquery asp.net-mvc

我需要在我的MVC项目中的每个10 Minuit中调用一个jquery函数。

setInterval(function () {

            $.ajax({
                type: "GET",
                cache: false,
                url: "/Home/dwnld_all",
                success: function (result) {
                    alert(result);
                }
            });
        }, 600000);

这是我的代码。但在这方面,当控件转到下一页时,它不起作用。这有什么jquery代码吗?

1 个答案:

答案 0 :(得分:0)

您的js仅限于当前页面。默认情况下,不能跨重新加载的页面访问它。您需要获取一些服务器端代码的帮助。像这样:

// Your controller action that you will be redirecting to
public ActionResult MyAction(int pendingInterval){
    ViewBag.Pending = pendingInterval;
    return View();
}

您现在可以将js修改为以下内容:

function keepRunning() {

        $.ajax({
            type: "GET",
            cache: false,
            url: "/Home/dwnld_all",
            success: function (result) {
                alert(result);
            }
        });
 }

在您的视图页面中,您可以使用以下代码初始运行keepRunning功能

setTimeout(function(){
    keepRunning() // run initially for the ending time
    setInterval(keepRunning, 600000);
}, @ViewBag.Pending);

现在,当您重定向页面时,您必须将剩余时间也传递给网址。