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
100
votes
10 answers

Bypass popup blocker on window.open when JQuery event.preventDefault() is set

I want to show a JQuery dialog conditionally on click event of an hyperlink . I have a requirement like on condition1 open a JQuery dialogue and if condition1 is not satisfied, navigate to the page as referenced by 'href' tag of whose click event…
mavaze
  • 1,197
  • 3
  • 9
  • 12
41
votes
6 answers

When would I use JQuery.Callbacks?

I was looking through new stuff added to jQuery 1.7 and I saw they now have jQuery.Callbacks() http://api.jquery.com/jQuery.Callbacks/. The documentation shows you how to use jQuery.callbacks() but not any applicable examples of when I would want…
Keith.Abramo
  • 6,820
  • 2
  • 26
  • 43
40
votes
2 answers

jQuery $.animate() multiple elements but only fire callback once

If you select a class or collection of elements to animate with jQuery: $('.myElems').animate({....}); And then also use the callback function, you end up with a lot of unneccessary animate() calls. var i=1; $('.myElems').animate({width:'200px'},…
totallyNotLizards
  • 7,979
  • 8
  • 46
  • 83
19
votes
5 answers

jQuery HOW TO?? pass additional parameters to success callback for $.ajax call?

I am trying, in vain it seems, to be able to pass additional parameters back to the success callback method that I have created for a successful ajax call. A little background. I have a page with a number of dynamically created textbox / selectbox…
dotnetgeek
  • 275
  • 1
  • 4
  • 14
18
votes
4 answers

Proper way to add a callback to jQuery DatePicker

DatePicker has an internal function, _adjustDate(), which I'd like to add a callback after. Current Method This is how I'm currently doing this. Basically, just replacing the function defintion with itself, plus the new operations. This effects…
vol7ron
  • 35,981
  • 19
  • 104
  • 164
15
votes
7 answers

How do I animate in jQuery without stacking callbacks?

Let's say I have three divs, and I'd like each to animate once the previous one is done. Currently, I write this: $('div1').fadeOut('slow', function() { $('div2').fadeOut('slow', function() { $('div3').fadeOut('slow'); }); }); Which…
Yuval Karmi
  • 24,847
  • 38
  • 116
  • 172
14
votes
2 answers

Jquery fadeOut/fadeIn callback not working

i m building a little script to animate a list. Here is my html structure:
13
votes
2 answers

What are jQuery hooks and callbacks?

I am having a tough time conceptualizing what exactly callbacks or hooks are in jQuery. They seem to be lumped together, but I don't know the difference between them. From what I understand from other posts about callbacks, like this, a callback is…
stinkycheeseman
  • 34,999
  • 7
  • 27
  • 49
13
votes
3 answers

jquery ajax ignores 500 status error

I'm making some GET requests to an App Engine app, testing in Chrome. Whilst I can see in javascript console that some calls result in a 500 server error, I can't seem to find anyway of capturing this error in my jQuery code despite reading a number…
oli
  • 3,401
  • 1
  • 25
  • 34
12
votes
4 answers

jQuery ajax callback never called

Javascript code, using jQuery 1.7: $( function() { $.get('/ajax_dummy', function() { alert('foo'); }) }); With Firebug I can see that the HTTP GET request is sent and a "hello world" response with code 200 is returned, so everything seems fine.…
Lucy Brennan
  • 555
  • 1
  • 7
  • 13
11
votes
3 answers

$(document).ready inside $(document).ready

I found code in my codebase that has $(document).ready(function() {...} inside of another $(document).ready(function() {...} e.g. $(document).ready(function() { // 20 lines... $(document).ready(function() { foo() } …
David Groomes
  • 2,115
  • 19
  • 20
10
votes
1 answer

closure inside a for loop - callback with loop variable as parameter

I am using jQuery "GET" in a loop to obtain several results from the server. I want to include the loop index as a fixed parameter to the call back but its not working. (I followed the advice of this article on how to do it.) However, the value I…
Danny
  • 2,221
  • 2
  • 24
  • 37
10
votes
2 answers

Jasmine test using .toHaveBeenCalledWith fails for sign-up form

The single page application I am working on has a login view with two forms: a sign-in form and a sign-up form. The following spec describes tests for these forms. I am using Jasmine-jQuery 1.4.2. // user_spec.js describe("User", function() { …
JJD
  • 44,755
  • 49
  • 183
  • 309
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,459
  • 4
  • 31
  • 36
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
  • 6,275
  • 4
  • 31
  • 44
1
2 3
26 27