最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery change event callback - Stack Overflow

programmeradmin2浏览0评论

How to call a function once after change() event complete?

for example, something like this: ( I know jQuery Doesn't have callback method as default )

$('#element').change( function(){
                     // do something on change
                     // $('#milestonesSelect').multiselect({ minWidth: 120 , height : '200px' ,selectedList: 4  }).multiselectfilter();
                      // some animation calls ...
                      // ...
                     }, function(){
                     // do something after complete
                      alert('another codes has completed when i called');
                     }
                   );

Is it possible to call a single callback after all the codes on change method done, except call a complete callback for every functions on it?

I need to do something after the change event has completed

Shall I need to set order to methods in change handler?

How to call a function once after change() event complete?

for example, something like this: ( I know jQuery Doesn't have callback method as default )

$('#element').change( function(){
                     // do something on change
                     // $('#milestonesSelect').multiselect({ minWidth: 120 , height : '200px' ,selectedList: 4  }).multiselectfilter();
                      // some animation calls ...
                      // ...
                     }, function(){
                     // do something after complete
                      alert('another codes has completed when i called');
                     }
                   );

Is it possible to call a single callback after all the codes on change method done, except call a complete callback for every functions on it?

I need to do something after the change event has completed

Shall I need to set order to methods in change handler?

Share Improve this question edited Apr 4, 2013 at 8:01 Alireza asked Apr 4, 2013 at 7:26 AlirezaAlireza 1,4384 gold badges21 silver badges33 bronze badges 3
  • Just run your code inside that change event. There's no callback needed. – elclanrs Commented Apr 4, 2013 at 7:28
  • 1 By 'do something after complete' do you mean once the first function has finished, or after the change event has completed? – David Thomas Commented Apr 4, 2013 at 7:32
  • after the change event has completed – Alireza Commented Apr 4, 2013 at 7:36
Add a comment  | 

3 Answers 3

Reset to default 11

You can probably make use of event bubbling and register a callback in the document.

$(document).on('change', '#element', function(){
    console.log('after all callbacks')
});

Demo: Fiddle

I just spent some time exploring this myself, and decided to submit my answer to this question as well. This is not utilizing any of jQuery's deferred methods, which might be a better approach. I haven't explored them enough to have an opinion on the matter.

$("#element").change(function() {
  handler().after(callBack($("#element").val()));
}

function handler() {
  alert("Event handler");
}

function callBack(foo) {
  alert(foo);
}

Demo: JSFiddle

If I understand your question correctly, this will execute code only one time after a change event is fired:

var changed = false;

$('#element').on('change', function() {
    if ( !changed ) {
        // do something on change
        changed = true;
    }
  }
});
发布评论

评论列表(0)

  1. 暂无评论