Actions Trigger Event Delay Setting (Auto Save Delay)

  • 1
  • Idea
  • Updated 2 years ago
I am trying to employ auto save to a page so that any field that is edited is automatically saved to the model. This is pretty easy through model actions, but the problem is that the save action is too quick. This is a particular issue for date fields if you are manually typing them in. Is there a way to delay this action. In the future, it might be a nice feature to have a delay setting on the Event Trigger so that any event trigger could be delayed a number of seconds before the action is executed. Any help on this would be appreciated.
Thanks
Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
You could introduce a delay to your Action sequence by adding a Run Snippet action at the beginning that delays the execution of actions in the sequence by a fixed amount of time. Here is what such a Snippet would look like: here I'm waiting 5 seconds but you can change that easily:

var SECONDS_TO_DELAY = 5;
var dfd = skuid.$.Deferred();
setTimeout(function(){
    dfd.resolve();
},SECONDS_TO_DELAY*1000);
return dfd.promise();

Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb
Many thanks! Works great!
Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb
Hi Zach... Follow up. Is there a way to set a delay based on seconds after last character typed? If someone is in a text field and typing a 15 word sentence, I would want the auto save to occur when they have stopped typing for 1 or 2 seconds. Is that possible?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yes this is possible - just a slight adjustment to the JavaScript so that we keep track of a timer which we will then reset every time the user types another character. In your Skuid Page, you need to change the Resource Location of the JavaScript Resource from Inline (Snippet) to Inline - very important! - and then replace the Resource Body with this:

(function(skuid){
var $ = skuid.$;
var timer;
skuid.snippet.registerSnippet('Wait5Seconds',function(){
   var SECONDS_TO_DELAY = 2;
        var dfd = skuid.$.Deferred();
        // Clear existing timer
        clearTimeout(timer);
        timer = setTimeout(function(){
            dfd.resolve();
        },SECONDS_TO_DELAY*1000);
        return dfd.promise(); 
});
})(skuid);


NOTE: I changed SECONDS_TO_DELAY in the snippet above to be 2 - but I left the Snippet name at "Wait5Seconds". You can change the Snippet Name, just make sure you change the Run Snippet action in your Model Action Sequence as well.

Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
By the way, I think 2 seconds is too fast --- it's not the best user experience. I'd back it up to at least 5, if not more. In my testing of this I found 2 seconds to be rather annoying, it doesn't even give you enough time to switch the field you're editing.
Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb
You are now in a tie with Rob for world's best Skuidery. Thanks!. The first code works perfectly, the second doesn't seem activate any delay. I made sure to switch to inline. I don't get any error messages, but the save occurs instantly.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Are you sure that in your Model Actions your "Run Skuid JavaScript Snippet" action is set to "Run Snippet: Wait5Seconds"?
Photo of Lauren Ake

Lauren Ake

  • 368 Points 250 badge 2x thumb
Sooo....I've tried both of these, and even tried only having the trigger happen on non-narrative fields, but it's still "glitchy." When users are typing in a text box, the form auto-saves and throws them out of where they are (sometimes removing the last few characters that they've typed). Ideas? I'm pretty sure adding this to my model is pretty idiot-proof but it's always entirely possible I'm doing something wrong...