Tip: Click lines to highlight, hold ctrl/cmd to multi-select

DateJS throttling (21-Jun @ 13:46)

remy

Syntax Highlighted Code

  1. function delay(fn, time) {
  2.     return function () {
  3.         var t = this;
  4.  
  5.         if (fn.timer) {
  6.             clearTimeout(fn.timer);
  7.         }
  8.         fn.timer = setTimeout(function () {
  9.             fn.apply(t);
  10.         }, time);
  11.     };
  12. }
  13.  
  14. $(function () {
  15.     var humanDate = $('#human_date').keyup(delay(function () {
  16.         if (humanDate.data('prevValue') == humanDate.val()) return;
  17.         humanDate.data('prevValue', humanDate.val());
  18.  
  19.         var date = '';
  20.         try {
  21.             date = Date.parse(this.value).toString("d-MMMM yyyy"); // note different formats available
  22.         } catch (e) {
  23.             date = 'Sorry, I cant do that date';
  24.         }
  25.  
  26.         $('#info').html(''); // error notice field
  27.         $('#actualDate').text(date); // date selected (i.e. in plain text)
  28.     }, 200));
  29.  
  30.     // force a trigger
  31.     humanDate.keyup();
  32. });

Plain Code

function delay(fn, time) {
    return function () {
        var t = this;

        if (fn.timer) {
            clearTimeout(fn.timer);
        }
        fn.timer = setTimeout(function () {
            fn.apply(t);
        }, time);
    };
}

$(function () {
    var humanDate = $('#human_date').keyup(delay(function () {
        if (humanDate.data('prevValue') == humanDate.val()) return;
        humanDate.data('prevValue', humanDate.val());

        var date = '';
        try {
            date = Date.parse(this.value).toString("d-MMMM yyyy"); // note different formats available
        } catch (e) {
            date = 'Sorry, I cant do that date';
        }

        $('#info').html(''); // error notice field
        $('#actualDate').text(date); // date selected (i.e. in plain text)
    }, 200));

    // force a trigger
    humanDate.keyup();
});

Codedump Run

Permalink: http://codedumper.com/datejs-throttling