6 Replies - 500 Views - Last Post: 24 February 2013 - 01:30 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

callback not working?

Posted 23 February 2013 - 04:39 PM

whats wrong with this callback? alert shows blank. (it works without adding the callback.)

function getNewest(){
	  var numbers=$("#history").html();
	  alert(numbers);
  } 

function update(callback){
  $.getJSON("http://domain.com/results.php",function(result){
         $("#history").append( ..........
  });
  callback();
};

$(document).ready(function(){
	update(getNewest); 	
	setInterval(areWeThereYet,60000);
	
});

function areWeThereYet()
{
     update(getNewest); 
}

This post has been edited by g37752: 23 February 2013 - 05:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: callback not working?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Re: callback not working?

Posted 24 February 2013 - 01:46 AM

why should the alert show something? when you call update() you issue an asynchronous AJAX request, and immediately after sending it off, you call getNewest() but since the AJAX request is still under way, there is nothing to display. some time after that call, the AJAX request returns and does its changes.
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: callback not working?

Posted 24 February 2013 - 10:05 AM

yes, and i thought I need to use callback because ajax is asynchronous. otherwise, i would just call 'getNewest' after i call 'update'.

so what's wrong with the callback?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: callback not working?

Posted 24 February 2013 - 10:14 AM

function update(callback) {
  $.getJSON("http://domain.com/results.php",function(result){
         $("#history").append( ..........
         callback();
  });
}

This post has been edited by andrewsw: 24 February 2013 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: callback not working?

Posted 24 February 2013 - 12:06 PM

View Postandrewsw, on 24 February 2013 - 10:14 AM, said:

function update(callback) {
  $.getJSON("http://domain.com/results.php",function(result){
         $("#history").append( ..........
         callback();
  });
}


thanks. it works. i thought about trying that, just didnt understand. so the callback has to be called within the ajax call? what if there is no ajax? i can still use callback. e.g.,

$("#.div").hide(callback)

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: callback not working?

Posted 24 February 2013 - 12:13 PM

Yes, most jQuery methods allow a callback, to be called when the method has completed. The docs for hide - the complete argument.

Your jQuery selector is incorrect. I know it's just thrown together for your post, but:

// either
$("#someid").
$(".someclass"). // or
$("div") // meaning all DIVs

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Re: callback not working?

Posted 24 February 2013 - 01:30 PM

View Postg37752, on 24 February 2013 - 06:05 PM, said:

yes, and i thought I need to use callback because ajax is asynchronous.

a callback and asynchronous excution are two completely separate issues. callbacks are functions that are passed around as variables (and can be executed anywhere, anytime). asynchronous means that a function is called now and executes in the backgound until it is finished, while the main programme proceeds (WebWorkers are a good example for that).

This post has been edited by Dormilich: 24 February 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1