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.

390 questions
10
votes
5 answers

sequencing function calls in javascript - are callbacks the only way?

I read through various threads like this one for example. But it really escapes me how to accomplish the following: I have 4 functions, and want them happen one after another in sequence. Notice they are in incorrect order, to get my point across. I…
tim
  • 3,823
  • 5
  • 34
  • 39
9
votes
0 answers

PhantomJS: Operation Canceled while loading resource via AJAX

This is my code: page.onResourceRequested = function (requestData, networkRequest) { console.log(requestData.url); // This message shows http://website-with-jquery-included.com/page.html! } page.onResourceError = function(resourceError) { …
Serhii Matrunchyk
  • 9,083
  • 6
  • 34
  • 47
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…
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
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
  • 18,766
  • 21
  • 107
  • 173
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,031
  • 1
  • 22
  • 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
  • 6,294
  • 16
  • 55
  • 88
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,832
  • 4
  • 19
  • 26
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,652
  • 14
  • 67
  • 96
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,855
  • 2
  • 18
  • 36
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,967
  • 2
  • 21
  • 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
  • 32,460
  • 9
  • 122
  • 129
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,647
  • 11
  • 71
  • 139
5
votes
2 answers

jQuery AJAX Request 302 Redirect - What callbacks are available?

I'm working with an older system that is using jQuery 1.2.6. I am sending an AJAX Request via the jQuery.ajax function. The URL that it is hitting is sending a 302 HTTP Redirect response and eventually ends up with a 200 HTTP OK response. I have…
John
  • 9,254
  • 12
  • 54
  • 75
5
votes
3 answers

jQuery get source element in callback

$('.myElem').live('click', function() { $(this).hide(500, function() { $(this).siblings('.myOtherElem').show(); }); }); The above doesn't work because $(this) is no longer in correct scope in the callback. How do I pass my original…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
1
2
3
25 26