使用Jquery设置计时器

使用Jquery设置计时器,jquery,timer,Jquery,Timer,这是我的设置。我有一个钮扣 <input type="submit" value="Checkout" name="submitAction" class="btn btn-block alert-success unlock" /> 在单击该按钮的同时,我正在尝试运行一个15分钟的jquery计时器,该计时器将让用户知道他们的会话已过期,并点击另一个控制器来解锁此案例 [HttpPost] public ActionResult UnlockCase(int id)

这是我的设置。我有一个钮扣

<input type="submit" value="Checkout" name="submitAction" class="btn btn-block alert-success unlock" />
在单击该按钮的同时,我正在尝试运行一个15分钟的jquery计时器,该计时器将让用户知道他们的会话已过期,并点击另一个控制器来解锁此案例

  [HttpPost]
    public ActionResult UnlockCase(int id)
    {
        Case currentCase = db.Cases.Find(id);
        currentCase.LockCase = false;

        string url = this.Request.UrlReferrer.AbsolutePath;
        return Redirect(url);
    }
我用JQuery尝试了很多不同的方法,但我似乎找不到正确的方法来实现这一点

     $('.unlock').click(function(){
    setTimeout(function () {
            var id = @Model.ID
            $.ajax({
                type: 'POST',
                url: '/Case/UnlockCase',
                data: 'id=' + id,
                success: function () {
                    window.location.reload(true);
                }
            });
            //}, 15 * 1000 * 60);
        }, 10000);
    }
错误:

不推荐使用getPreventDefault。防止使用默认值 相反browserLink:37:0在内容处理中使用nsIFile是不正确的 不赞成。filesystem.js:38:0类型错误:this.\u recipeManager为空 jsm:185:9不推荐使用getPreventDefault。 改用defaultPrevented。browserLink:37:0处理程序函数已抛出 异常:TypeError:info为空堆栈: _addBreakpoint@resource://gre/modules/commonjs/toolkit/loader.js->resource:///modules/devtools/sourceeditor/debugger.js:138:5 添加断点/ resource:///modules/devtools/sourceeditor/debugger.js:154:37 使无误/ resource://gre/modules/devtools/DevToolsUtils.js:83:14 第138行, 列:5 DevToolsUtils.js:59:0键事件在某些服务器上不可用 键盘布局:键=e修改器=accel alt debugger.xul键事件 在某些键盘布局上不可用:key=v modifiers=accel alt debugger.xul键事件在某些键盘布局上不可用:键=f modifiers=accel alt debugger.xul键事件在GTK2上不可用: 键=u修改器=移位,加速工具箱.xul


任何帮助都将不胜感激。

将按钮类型更改为“按钮”。提交通常用于将表单提交到后端


我想你误解了setTimeout事件。事件将首先等待计时器完成,然后开始执行内部定义的函数。在您的情况下,这意味着要比jquery请求等待15分钟。看

我想你需要的是这样的:

            // Set the time for error message, if request woudn't end
            var yourTimeout = setTimeout(function() { timeOutError() }, 10000);
            var id = @Model.ID
            $.ajax({
                type: 'POST',
                url: '/Case/UnlockCase',
                data: 'id=' + id,
                success: function () {
                    window.location.reload(true);
                    //clear timeout on success
                    clearTimeout(yourTimeout);
                },
                error() {
                    //handle erre 
                    //clear timeout on error
                    clearTimeout(yourTimeout);
                }
            });

            function timeOutError() {
                // your timeout error 
            }
根据您的评论进行更新


控制台中是否有错误?尝试使用浏览器开发工具查看到底发生了什么。这不是因为我经常出错,而是因为我不知道如何定位我的Jquery,以便计时器在单击时启动,完成后点击控制器中的下一个操作。谢谢DDan。我遇到的问题是,它直接进入我的“解锁”操作。我希望它点击“CheckoutCase”操作,等待15分钟,然后点击我的“UnlockCase”操作。在这种情况下,它看起来就像更新的代码。onclick启动计时器并调用CheckoutCase。如果没有超时,则为ok。如果超时,调用UnlockCase我是否需要更改按钮,因为每个按钮都会触动控制器?我正在通过“CheckOutCase”控制器,但在它点击“timeOutAction”函数之前,它出现了某种javascript错误。第二个ajax调用在我的第二个ajax调用中没有关闭。我更新了。顺便说一句,错误是什么?是的,我之前看到过。。。让我看看我是否能深入研究并得到一个具体的错误。
            // Set the time for error message, if request woudn't end
            var yourTimeout = setTimeout(function() { timeOutError() }, 10000);
            var id = @Model.ID
            $.ajax({
                type: 'POST',
                url: '/Case/UnlockCase',
                data: 'id=' + id,
                success: function () {
                    window.location.reload(true);
                    //clear timeout on success
                    clearTimeout(yourTimeout);
                },
                error() {
                    //handle erre 
                    //clear timeout on error
                    clearTimeout(yourTimeout);
                }
            });

            function timeOutError() {
                // your timeout error 
            }
         $('.unlock').click(function(){
            var id = @Model.ID
            // Set the time for error message, if request woudn't end
            var yourTimeout = setTimeout(function() { timeOutAction(id) }, 10000);
            // ajax request to CheckOutCase
            $.ajax({
                type: 'POST',
                url: '/Case/CheckoutCase',
                data: 'id=' + id,
                success: function (result) {
                    window.location.reload(true);
                    //clear timeout on success
                    clearTimeout(yourTimeout);
                },
                error: function(requestObject, error, errorThrown) {
                    //handle erre 
                    //clear timeout on error
                    clearTimeout(yourTimeout);
                }
            });

         });

         // invoked because of timeout
         function timeOutAction(id) {
             // ajax request to UnlockCase
             $.ajax({
                type: 'POST',
                url: '/Case/UnlockCase',
                data: 'id=' + id,
                success: function (result) {

                },
                error: function(requestObject, error, errorThrown) {

                }
             });
         }