Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input id="txt" type="text" />
- <span id="changeCount">0</span>
- $('#txt').keydown(function(event) {
- // Don't count the keys which don't actually change
- // the text. The four below are the arrow keys, but
- // there are more that I omitted for brevity.
- if (event.which != 37 && event.which != 38 &&
- event.which != 39 && event.which != 40) {
- // Replace the two lines below with whatever you want to
- // do when the text changes.
- var count = parseInt($('#changeCount').text(), 10) + 1;
- $('#changeCount').text(count);
- }
- });
- var i = 0;
- $('#text').bind('check_changed', function(){
- var t = $(this);
- // do something after certain interval, for better performance
- delayRun('my_text', function(){
- var pv = t.data('prev_val');
- // if previous value is undefined or not equals to the current value then blablabla
- if(pv == undefined || pv != t.val()){
- $('#count').html(++i);
- t.data('prev_val', t.val());
- }
- }, 1000);
- })
- // if the textbox is changed via typing
- .keydown(function(){$(this).trigger('check_changed')})
- // if the textbox is changed via 'paste' action from mouse context menu
- .bind('paste', function(){$(this).trigger('check_changed')});
- // clicking the flush button can force all pending functions to be run immediately
- // e.g., if you want to submit the form, all delayed functions or validations should be called before submitting.
- // delayRun.flush() is the method for this purpose
- $('#flush').click(function(){ delayRun.flush(); });
- ;(function(g){
- var delayRuns = {};
- var allFuncs = {};
- g.delayRun = function(id, func, delay){
- if(delay == undefined) delay = 200;
- if(delayRuns[id] != null){
- clearTimeout(delayRuns[id]);
- delete delayRuns[id];
- delete allFuncs[id];
- }
- allFuncs[id] = func;
- delayRuns[id] = setTimeout(function(){
- func();
- delete allFuncs[id];
- delete delayRuns[id];
- }, delay);
- };
- g.delayRun.flush = function(){
- for(var i in delayRuns){
- if(delayRuns.hasOwnProperty(i)){
- clearTimeout(delayRuns[i]);
- allFuncs[i]();
- delete delayRuns[i];
- delete allFuncs[i];
- }
- }
- };
- })(window);
Add Comment
Please, Sign In to add comment