0

Arrghh, I know there is ways to do this but it just won't work for me.

I have a page, that on load light up icons if they exist in my database.

Icons that are still are disabled have a onclick-function connected to them. This onclick-function then calls another function that adds the icon to my database (which is a asynchronous function). When this asynchronous function is done I want to reload the page so that my icons light up again, this time with the new icon that were added to my database.

It does reload, but to fast. It doesnt seem to wait for the asynchronous function to be done. In other words my icon wont light up on click, but when I refreshes my page manually after I have clicked it, the icon lights up.

Here is my function that I think should work.

$.when(addUserToNetwork(response.email, 2, response.id)).done(function(){location.reload();});

This:

addUserToNetwork()

is my asynchronous function. When this is .done I want to reload the page.

What am I doing wrong?

Here's my code for addUserToNetwork function():

 function addUserToNetwork(_email, _networkID, _idAtNetwork){
   return $.ajax({
      url: '/fb/updateUserNetwork',
      type: 'POST',
      contentType: 'application/json',
      data: JSON.stringify({email:_email, networkID:_networkID, idAtNetwork:_idAtNetwork}),
      dataType: 'xml/html/script/json'
    });
 }
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
malmling
  • 2,398
  • 4
  • 19
  • 33
  • 1
    You'll have to show the content of `addUserToNetwork`. Are you returning a deferred? That's what `when` expects. – T.J. Crowder Mar 27 '13 at 10:15
  • is it working if you just put a alert or something – Chamika Sandamal Mar 27 '13 at 10:15
  • Sorry I forgot to tell what is going wrong. It does reload, but to fast. It doesnt seem to wait for the asynchronous function to be done. In other words my icon wont light up on click, but when I refreshes my page manually after I have clicked it, the icon lights up. – malmling Mar 27 '13 at 10:19
  • @T.J.Crowder , if I dont return a deferred. Might there be any other method to use than $.when? – malmling Mar 27 '13 at 10:22
  • 1
    @RobertAxelsson your code is (mostly) correct, although the extra `$.when` wrapper is superfluous. You should add some `console.log` lines to try to prove to yourself that the `.done` handler is indeed waiting for the AJAX call to complete. – Alnitak Mar 27 '13 at 10:42
  • @RobertAxelsson: Or better yet, set breakpoints and walk through the code with a debugger. There's one built into your browser, assuming you're using a remotely current browser. – T.J. Crowder Mar 27 '13 at 10:46
  • @T.J.Crowder breakpoints and async logic often don't mix :( – Alnitak Mar 27 '13 at 10:51
  • I'm sorry. Gonna put my comment in my question. – malmling Mar 27 '13 at 10:56
  • @Alnitak: They work fine if you put the breakpoints in the right place (e.g., the callback). – T.J. Crowder Mar 27 '13 at 10:57
  • @T.J.Crowder true - he should at least be able to see the network activity if using Chrome or similar – Alnitak Mar 27 '13 at 10:59
  • possible duplicate of [jQuery ajax Page Reload](http://stackoverflow.com/questions/14632753/jquery-ajax-page-reload) – Paul Sweatte Jan 15 '14 at 02:25

0 Answers0