Questions tagged [jquery-callback]

A callback function (in JavaScript) is a function, passed as an argument, that is invoked if a certain event is picked up on, or after a predefined period of time. jQuery callbacks are exactly the same as JavaScript callbacks, except that jQuery invokes the callback in a set context that sometimes differs from the one in vanillaJS.

In programming, a callback is a piece of executable code that is registered to be invoked as a response to certain events. This can be anything ranging from an interupt, a certain system signal being received to the more high-level user events that are used in jQuery.

jQuery callbacks are exactly the same as JavaScript callbacks, apart from one thing: JavaScript invokes callback functions either in the global (window) context, or the context of a particular DOM element. The latter being relevant to callbacks (often referred to as handlers) passed to the addEventListener method.
When using techniques like event delegation, a regular JavaScript handler will invoke the callback in the context of the DOM element to which the listener is attached. jQuery will invoke the callback in the context of the event's target.
An example of this distinction:

$('body').on('click', 'a', function()
{
    console.log(this);//refers to the link that was clicked
});
document.body.addEventListener('click', function(e)
{
    var target = e.target || e.srcElement;
    if (target.tagName.toLowerCase() === 'a')
    {
        console.log(this);//logs body, that's where the listener was bound
        console.log(target);//logs the clicked link
    }
}, false);

Other examples of callbacks are functions passed to setTimeout or setInterval. These functions are put in a queue, waiting for the pre-set interval to pass, and are then invoked by the JS engine, if, and only if, the JS engine is free at that time.
The programmer has no real control over the order in which the queue is processed, in case several event handlers are queued, it could be that first all the handlers are invoked, and then the timeout callback gets its turn, or the other way around.

jQuery let's programmers create their own queue, using $.queue, which they can control.

398 questions
9
votes
2 answers

Pass arguments to a callback function in jquery click event

Straight to the business: I have a jquery event listener that looks like this: $(".number").click(printNumber); and a callback function: function printNumber(number){ console.log(number); } I was wondering if I could pass an argument to a…
sheriff_paul
  • 609
  • 1
  • 9
  • 20
9
votes
3 answers

jquery change event callback

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 …
Alireza41
  • 1,387
  • 4
  • 20
  • 32
9
votes
3 answers

Why does jquery ajax callback function not work?

I am having a problem with a simple callback function in jQuery ajax. Google won't help and stack overflow wouldn't either, so I guess this might not be something specific but rather something I am too ignorant to see. To me the code looks exactly…
weltschmerz
  • 12,320
  • 9
  • 57
  • 109
8
votes
2 answers

How to track lists of nodes in a Jquery plugin (if clients may remove those DOM nodes)

I'm writing a jQuery plugin that "points at" a certain number of nodes in the DOM. Yet if I try something like having my plugin hold references to a set of nodes, I worry about them going "stale". (While I realize that JavaScript is garbage…
8
votes
5 answers

Correct way to bind "this" in JavaScript event callbacks?

I created a class called SearchBox to handle search interaction (delayed trigger, search on enter key press, preventing searches while one is active, synchronizing results when a search completes and the text has changed, etc.). All the class…
Triynko
  • 17,370
  • 20
  • 92
  • 154
8
votes
4 answers

Delay form submission until location is retrieved

I have an application that attempts to get locations settings from the browser, this tends to take a little while so I want it to run when the page loads. However, if you click the submit button before the location callback has run, you have no…
rooftop
  • 3,005
  • 1
  • 19
  • 33
7
votes
2 answers

jQuery Callback and Pub / Sub

In the past I have done very simple pub / sub in jQuery by binding on the window. // subscribe $( window ).on("someEvent", function() { ... }); // publish $( window ).trigger("someEvent"); However I recently learned about the new callbacks…
user1031947
  • 5,172
  • 14
  • 48
  • 76
7
votes
2 answers

jQuery. How is queue() different from using callback function for something is done?

html: hello world! js: (using callback) $('span').click(function() { $(this).animate({ fontSize: '+=10px' }, 'slow', function() { // callback after fontsize increased $(this).text( $(this).text() + ' rolled! ' ); …
gilzero
  • 1,667
  • 3
  • 17
  • 25
6
votes
2 answers

Programming a callback function within a jQuery plugin

I'm writing a jQuery plug-in so I can reuse this code in many places as it is a very well used piece of code, the code itself adds a new line to a table which has been cloned from a hidden row, it continues to perform a load of manipulations on the…
Ben Everard
  • 13,254
  • 12
  • 63
  • 95
6
votes
2 answers

If a jQuery function calls itself in its completion callback, is that a recursive danger to the stack?

I'm writing a little jQuery component that animates in response to button presses and also should go automatically as well. I was just wondering if this function recursive or not, I can't quite work it out. function animate_next_internal() { …
NXT
  • 1,825
  • 2
  • 18
  • 35
6
votes
1 answer

Jquery Trigger Events programmatically and wait until the action of previous event is complete to fire next one

I am novice to jquery. Suppose i have a list of 10 "a" tags all attached to an event handler mouseover, click, mouseout respectively. what i want to do is iterate over all the "a" elements and trigger these events using jquery trigger. The issue i…
jaipster
  • 11,767
  • 2
  • 19
  • 24
6
votes
1 answer

How to extend jQuery's animate-step function

Any ideas how to extend the step-function in jQuery 1.6+? I've made a special-event to trigger a custom-event on each animated step. However since jQuery's animation method was changed, or rather the step function is not longer extendable ($.fx.step…
yckart
  • 28,174
  • 7
  • 112
  • 121
6
votes
3 answers

jQuery AJAX custom function and custom callback?

Heylow everyone! I have an ajax() call like so: $.ajax({ type: "post", url: "whatever.php", data: { theData: "moo moo" }, success: function(data) { console.log(data); } }); Is it possible to wrap this inside a custom function…
Barrie Reader
  • 10,474
  • 11
  • 67
  • 132
5
votes
1 answer

Rails Ajax, success callback data undefined

So I'm fairly new to Rails. Heres is my issue: I have an Ajax call in my coffeescript to a Tenant model, edit page. $("#tenant_tenantbuildinginfo").change -> $.ajax url: "/buildings/getgst", dataType: "json", data:…
msadoon
  • 143
  • 2
  • 11
5
votes
2 answers

jQuery Callback function won't take name of function(), must have an anonymous function

This works: $("#formbottom").slideUp(speed,'swing',function(){ openSubmitting(); }); This doesn't: $("#formbottom").slideUp(speed,'swing', openSubmitting() ); When you have a callback, do you always have to have an anonymous function in…
Doug Cassidy
  • 1,500
  • 2
  • 15
  • 25
1
2
3
26 27