0

I try to make a recursive function to delete files using jQuery and ajax to show the status of the process. The problem is, the first time the function is executed (index 0) everything is fine and the "output" is "1594.jpg". But when the index is incremented to 1, it's the second caracter of the chain filesToDelete, '5', then '9', and so on.

filesToDelete = new Array();
filesToDelete = '1594.jpg,my.png,test.ogg,Song.mp3,Apple_4S.mov,An App.exe';
filesToDelete = filesToDelete.split(',');
function ajaxDelete(itemList, cpt) {
 var tempItemList = itemList;
 //If cpt is not initialized, initialize it, make sure it's an integer
 if(isNaN(cpt)) cpt = 0*1;
 //If cpt is equal to the array, exit
 if(cpt >= itemList.length) return;
 //Current index
 var curIndex = cpt;
 //The value of the current index
 var current = itemList[curIndex];
 //DEBUG
 console.log('cpt:'+cpt+' index:'+curIndex+' Value:'+itemList[curIndex]);
 $.ajax({
 url: "deleteFile.php?id="+current,
 beforeSend: function(){
 progressText('Suppression de '+current);
 },
 success: function(data){
 progressText('Suppression...');
 //Index + 1
 cpt++;
 //RECURTION
 setTimeout("ajaxDelete('"+itemList+"', "+cpt+")",1);
 }
 });
}

Any help is welcomed, with explanation if possible..

Thanks !

asked Oct 9, 2011 at 5:06
4
  • How are you calling the ajaxDelete() with filesToDelete? Commented Oct 9, 2011 at 5:15
  • 2
    Wouldn't this be better done on the server directly? Commented Oct 9, 2011 at 5:16
  • @deceze: No because I want a feedback for the user and event if I use flush(*) in php, AJAX still wait for the complete page to be load before returning the result. Commented Oct 9, 2011 at 5:37
  • @Jared: I'm not calling it actually :), but I think I should because it will take less memory if the array got like 200 indexes Commented Oct 9, 2011 at 5:40

3 Answers 3

1

Don't rely on the ajax success method for this, if it fails for whatever reason your stuck.

You should do an ajax call per file to delete or better yet send the Array of files to the server and let it take care of it.

for (i = 0; i <= filesToDelete.length; i++)
{
 // Ajax Call
 $.ajax({
 });
}
answered Oct 9, 2011 at 5:16
Sign up to request clarification or add additional context in comments.

2 Comments

Oh damn, never thought of that, thank you so much, trying it right away!
Working like a charm! Thx man! EDIT: Is it possible to wait for an answer from the ajax request before skipping to the other index?
1

You're passing in itemList as a string, but believe it to be an array. (In the setTimeout call, I mean.)

But I agree with the comment/answer; ew!

answered Oct 9, 2011 at 5:16

1 Comment

How can I paste it as an array? (just for learning, I'll take Miste Jeremy solution :))
1

Your setTimeout call is executing a string command:

setTimeout("ajaxDelete('"+itemList+"', "+cpt+")",1);

When you concatenate strings and objects, it takes the value of toString() on that object. In the case of an array, that will give you this:

ajaxDelete('1594.jpg,my.png,test.ogg,Song.mp3,Apple_4S.mov,An App.exe', 1)

And strings will accept brackets and give you that character. This will give you '5'

'1594.jpg,my.png,test.ogg,Song.mp3,Apple_4S.mov,An App.exe'[1]

You could omit that parameter and use filesToDelete directly in your call...

answered Oct 9, 2011 at 5:24

6 Comments

Thanks Jason fot the explanation, now that you have explained it to me, I feel stupid XD... Next time I'll be more careful, thank you
No sorry Jason still getting an error from FireBug: setTimeout("ajaxDelete("+filesToDelete+", "+cpt+")",1);
I meant don't even take itemList as a parameter, declare your function "ajaxDelete(cpt)" and you can use the existing filesToDelete array. Since you just set the value outside your function it should be on your window object.
Okay tahnk you! But let say I can't, how should I parse the array in the function unsing ajaxDelete(...) ?
Maybe you could consider that parameter to be the comma-separated value string, and split it into a NEW array varaible inside your function? Then you're passing it as a string every time..
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.