0

Heres my set up. I have a button.

<input type="submit" value="Checkout" name="submitAction" class="btn btn-block alert-success unlock" />

That hits this controller.

 [HttpPost]
    public ActionResult CheckoutCase(int id)
  {
      Case currentCase = db.Cases.Find(id);
        currentCase.LockCase = true;
        currentCase.Lockout_TS = DateTime.Now;
        LogHistory userComments = new LogHistory("Case has been checked out", "User");
        currentCase.LogHistories.Add(userComments);
       db.SaveChanges();
        string url = this.Request.UrlReferrer.AbsolutePath;
        return Redirect(url);
    }

At the same time when that button is clicked I'm trying to run a 15 minute jquery timer that will let the user know that their session has expired and hit another controller that unlocks the case here.

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

        string url = this.Request.UrlReferrer.AbsolutePath;
        return Redirect(url);
    }

I've tried many different things with JQuery but I cant seem to find the right way to make this work.

     $('.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);
    }

Error:

Use of getPreventDefault() is deprecated. Use defaultPrevented instead. browserLink:37:0 Use of nsIFile in content process is deprecated. filesystem.js:38:0 TypeError: this._recipeManager is null LoginManagerParent.jsm:185:9 Use of getPreventDefault() is deprecated. Use defaultPrevented instead. browserLink:37:0 Handler function threw an exception: TypeError: info is null Stack: _addBreakpoint@resource://gre/modules/commonjs/toolkit/loader.js -> resource:///modules/devtools/sourceeditor/debugger.js:138:5 addBreakpoint/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource:///modules/devtools/sourceeditor/debugger.js:154:37 makeInfallible/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/DevToolsUtils.js:83:14 Line: 138, column: 5 DevToolsUtils.js:59:0 Key event not available on some keyboard layouts: key="e" modifiers="accel alt" debugger.xul Key event not available on some keyboard layouts: key="v" modifiers="accel alt" debugger.xul Key event not available on some keyboard layouts: key="f" modifiers="accel alt" debugger.xul Key event not available on GTK2: key="u" modifiers="shift, accel" toolbox.xul

Any help would be much appreciated.

2
  • Are you getting errors in console? Try using your browsers dev tools to see what exactly is happening.
    – mattfetz
    Sep 2, 2015 at 15:12
  • It's not that I'm getting errors so much, It's that I don't know how to situate my Jquery so that the timer starts on click and when it's done hits my next action in the controller.
    – brachen33
    Sep 2, 2015 at 15:16

2 Answers 2

0

Change the type of your button to "button". "submit" is usually used to submit forms to the backend.

<input type="button" value="Checkout" name="submitAction" class="btn btn-block alert-success unlock" />

0

I think you misunderstood the setTimeout event. The event will wait first until the timer is done, and then starts to execute the function defined inside. In your case that would mean, waits for 15 minutes than does the jquery request. See documentation

I think what you would need would goes somehow like this:

            // 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 
            }

UPDATE (base on your comment)

         $('.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) {

                }
             });
         }
6
  • Thanks DDan. The problem I'm having with this is that it goes straight to my 'UnlockCase' action. I want it onclick to hit the 'CheckoutCase' action, wait fifteen minutes, then hit my 'UnlockCase' action
    – brachen33
    Sep 2, 2015 at 16:06
  • In that case it would look somehow like the updated code. onclick starts the timer and calls CheckoutCase. If no timeout ok. If timeout, calls UnlockCase
    – DDan
    Sep 2, 2015 at 16:17
  • Do I need to change my button since each of these hit the controller? I'm making it past the 'CheckOutCase' Controller but it is getting somesort of javascript error before it hits the 'timeOutAction' function.
    – brachen33
    Sep 2, 2015 at 17:02
  • The second ajax call wasn't closed in my second ajax call. I updated. What is the error BTW?
    – DDan
    Sep 2, 2015 at 17:07
  • Yeah I saw that earlier... That me see if I can dig in and get a specific error.
    – brachen33
    Sep 2, 2015 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.