0

How how do I delete a jQuery Deferred from an array when it finishes? I have:

myarray[key]=$.ajax({

}).done(function(response){

 // work with response,
 // then delete myarray[key];

});

This does not work:

var myvar=function(){
  if(myvar){
    alert(1);
  }
} 

Seems like the declaration has to finish for an event of checking whether the variable is set or not, so delete of the variable inside the variable declaration shouldn't work How to, with a timeout?

1
  • Why not just delete myarray[key] ? Your problem isn't clear. Can you post more code so that we don't have to shoot in the dark (many possible problems here) ? Commented Oct 5, 2012 at 18:58

1 Answer 1

1

This depends on whether or not myarray is an Array or an Object. If it's an Object, you could just do

delete myarray[key];

But if it's an Array, then it's a little more complicated. John Resig wrote post about Array element removal here

If you want to do it more simply, and it's an Array, you could remove it with

myarray[key] = undefined;

But you should note that myarray doesn't get any smaller when you do this.

$.ajax returns a Deferred object, and .done() returns a different Deferred object. Optimally, you'd do the following:

myarray[key]=$.ajax({

});
myarray[key].done(function(response){
 // work with response,
 // then delete myarray[key];
});
Sign up to request clarification or add additional context in comments.

1 Comment

Will the code get properly executed at an ajax .done inside the very same array key being declared? my assumption it won't work has to do with what I posted as not working in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.