Guest User

Untitled

a guest
Jul 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. <input id="txt" type="text" />
  2. <span id="changeCount">0</span>
  3.  
  4. $('#txt').keydown(function(event) {
  5. // Don't count the keys which don't actually change
  6. // the text. The four below are the arrow keys, but
  7. // there are more that I omitted for brevity.
  8. if (event.which != 37 && event.which != 38 &&
  9. event.which != 39 && event.which != 40) {
  10.  
  11. // Replace the two lines below with whatever you want to
  12. // do when the text changes.
  13. var count = parseInt($('#changeCount').text(), 10) + 1;
  14. $('#changeCount').text(count);
  15.  
  16. }
  17. });
  18.  
  19. var i = 0;
  20. $('#text').bind('check_changed', function(){
  21. var t = $(this);
  22.  
  23. // do something after certain interval, for better performance
  24. delayRun('my_text', function(){
  25. var pv = t.data('prev_val');
  26.  
  27. // if previous value is undefined or not equals to the current value then blablabla
  28. if(pv == undefined || pv != t.val()){
  29. $('#count').html(++i);
  30. t.data('prev_val', t.val());
  31. }
  32. }, 1000);
  33. })
  34. // if the textbox is changed via typing
  35. .keydown(function(){$(this).trigger('check_changed')})
  36. // if the textbox is changed via 'paste' action from mouse context menu
  37. .bind('paste', function(){$(this).trigger('check_changed')});
  38.  
  39. // clicking the flush button can force all pending functions to be run immediately
  40. // e.g., if you want to submit the form, all delayed functions or validations should be called before submitting.
  41. // delayRun.flush() is the method for this purpose
  42. $('#flush').click(function(){ delayRun.flush(); });
  43.  
  44. ;(function(g){
  45. var delayRuns = {};
  46. var allFuncs = {};
  47.  
  48. g.delayRun = function(id, func, delay){
  49. if(delay == undefined) delay = 200;
  50. if(delayRuns[id] != null){
  51. clearTimeout(delayRuns[id]);
  52. delete delayRuns[id];
  53. delete allFuncs[id];
  54. }
  55. allFuncs[id] = func;
  56. delayRuns[id] = setTimeout(function(){
  57. func();
  58. delete allFuncs[id];
  59. delete delayRuns[id];
  60. }, delay);
  61. };
  62.  
  63. g.delayRun.flush = function(){
  64. for(var i in delayRuns){
  65. if(delayRuns.hasOwnProperty(i)){
  66. clearTimeout(delayRuns[i]);
  67. allFuncs[i]();
  68. delete delayRuns[i];
  69. delete allFuncs[i];
  70. }
  71. }
  72. };
  73. })(window);
Add Comment
Please, Sign In to add comment