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
0
votes
1 answer

Benefit to using jquery's .done()

Possible Duplicate: jQuery.ajax handling continue responses: “success:” vs “.done”? Is there any benefit to using jquery's done method compared to the success callback ? As far as I can tell these would both execute similar (if not the…
Matt
  • 1,225
  • 16
  • 23
0
votes
1 answer

ajax callback data [object Object] instead of text

i want to catch the data from server as an answer and alert it. but it is alerting [object Object] instead of my answer text "very good". here is my ajax function: function ajaxsubmit(){ $.ajax({ url: "/update", type: "POST", …
doniyor
  • 31,751
  • 50
  • 146
  • 233
0
votes
2 answers

I cannot access javascript object array property contents populated by callback that are visible in developer tools

Within a Javascript object (testObject) I am populating an array with objects from a jQuery.getJSON callback function. The array is defined first as an object property this.publicArray and var passedArray = this enables the inner callback function…
JSDBroughton
  • 3,688
  • 4
  • 29
  • 49
0
votes
1 answer

accessing outer scope in jQuery callback

I have a situation where I need a jQuery callback to work on a variable outside of it's scope. For the sake of simplicity, assume the following code: $('#myBtn').on('click', function(e) { var num = 1; // assume this returns the following: …
sa125
  • 25,703
  • 36
  • 105
  • 149
0
votes
2 answers

JavaScript window.close not firing after clicked on PrettyPhoto

JavaScript event onClick close window not firing after clicking and viewing PrettyPhoto image. HTML JavaScript function closeWin() { …
crashtestxxx
  • 1,224
  • 5
  • 19
  • 28
0
votes
0 answers

ASMX Web Service - Client listening to CallBack url

I have an asmx web service that I must consume passing him a callback url. Webservice use this url to make some http get passing some parameters in querystring with some info i need (Percentage,status, etcetc)... I can consume the webservice but I'm…
JDIBO
  • 55
  • 1
  • 8
0
votes
1 answer

Ajax call returning "0000000154" as 108

In my web application I am generating a serial number using ajax. Serial Number is always 10 digit long. So, if its less than 10 digit "0"'s are appended at the begining. I am doing an ajax call and the method is returning the serial number as 108…
ashishjmeshram
  • 11,077
  • 50
  • 148
  • 224
-1
votes
1 answer

How to Determine if There Are Any Un-Resolved Promises, Deferreds or Callbacks using jQuery?

Is there way to determine if there are any unsolved promises, deferreds or callbacks in jQuery? We have a large single-page JavaScript application that is hanging on $(document).ready(). For debugging purposes we would like to see if we missed…
A2MetalCore
  • 1,394
  • 2
  • 22
  • 42
-1
votes
1 answer

How wait for return value from $.getJson

I want to return a value from a getJson function, but the event does not take place, since getJSON wait. I tried realized callback function but result is same. My code is function getFBName(callback) { …
Hüseyin Okumuş
  • 137
  • 1
  • 2
  • 15
-1
votes
2 answers

calling a function having callback in for loop

I want to run window.resolveLocalFileSystemURI(file,success,fail) in for loop passing different file entries and want to return resolved entries in array only after I get all the entries. function resolveFiles(result,callback) { var…
N.Moudgil
  • 509
  • 2
  • 10
-1
votes
1 answer

How to load html file into div element then use elements from that html

When I am loading html file into div element and then I am trying to use elements from that html I can't find them. I am doing it in this way : var View = (function() { var instance = null; var IMG_ARROW_UP =…
-1
votes
2 answers

Pass variable to callback method

This question has little to do with jQueryUI's dialog, and more to do with JavaScript. I pass some variables to ayb.dialogError(), and use them to create a dialog. Then I pass different values, but the first are still used. The below script is…
user1032531
  • 24,028
  • 57
  • 174
  • 325
-1
votes
1 answer

navigator.geolocation and callback of callback

Initially, I had a code that worked: function get_coords(callback) { //If HTML5 Geolocation Is Supported In This Browser if (navigator.geolocation) { //Use HTML5 Geolocation API To Get Current Position …
rom
  • 3,224
  • 5
  • 33
  • 64
-1
votes
1 answer

Jquery ajax function to return data

I've read around 20 question on this specific question but couldn't get any of them to work. I'm creating a simple jquery function to query the iTunes api to get song data. I can't seem to get the data outside of the ajax function. I tried with…
-1
votes
4 answers

Is this a correct way of JavaScript/jQuery callback?

This is a simple question. Here is my code: $(document).ready( function () { func1( "foo", callback); function callback(param){ alert(param+" is my name"); } function func1(name, cb) { cb(name); // alerts…
SASM
  • 1,206
  • 1
  • 19
  • 43
1 2 3
26
27