I created a recursive loop to delete items in a SharePoint list with SPServices. I put a timeout in for 5 seconds; however for some reason it only deletes 2 items at a time, and then executes the wait instead of deleting the batch, and then repeating the function.
Please find my code below:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$intStart = 1;
$intCount = 10;
$intEnd = 40;
$("input[class='buttonpush']").click(function () {
var looper = function () {
if ($intStart < $intCount && $intStart < $intEnd) {
$().SPServices.SPUpdateMultipleListItems({
webURL: "http://widgettest",
listName: "TestList",
CAMLQuery: "<Query><Where><And><Gt><FieldRef Name='ID' /><Value Type='Counter'>" + $intStart + "</Value></Gt><Lt><FieldRef Name='ID' /><Value Type='Counter'>" + $intCount + "</Value></Lt></And></Where></Query>",
batchCmd: "Delete",
valuepairs: [],
debug: false,
completefunc: function (xData, Status) {
$intStart = $intStart + 5;
$intCount = $intCount + 5
setTimeout(looper, 5000);
}
});
}
else {
}
}
looper();
});
});
</script>
OK, now that you show the initial call to
looper(), there does not appear to be anything wrong with the logic.looper()should run 8 times until$intStartis equal to 41. These are the reasons that you might not get as many successive runs of looper() as you are expecting:$().SPServices.SPUpdateMultipleListItems()andcompleteFuncis not executed like you are expecting.$intStart,$intCountor$intEndso they get changed accidentally while you are running and thus the loop doesn’t run as many iterations as expected.looper()that causes execution to stop.To debug this, just put a unique
console.log()statement right before the call to$().SPServices.SPUpdateMultipleListItems()and another one in thecompleteFuncso you can tell exactly how far it’s getting before stopping.Some other observations on the code:
$intStartis always less than$intCountsince it starts less and both are incremented by 5 so you don’t need that part of theifstatement.$sign unless you are connoting some special kind of variable. That is not the case here and just impedes the readability of the javascript to someone familiar with javascript.$intStart,$intCountor$intEndshould probably not be global variables and in all cases should not be implicitly declared or accidentally declared globals. If you mean for them to be globals, then declare them as globals in the global scope usingvar. If you mean for them to be local to some scope, then declare them in that scope.document.ready()time, your button push will only work the first time. It will never do anything when pushed a second time.looper()is running (this will cause duplicate requests to be launched).Here’s what your CAMLQuery will be the first two times:
This doesn’t look correct to me. The first time, you’re asking to process > 1 && < 10. The second time, you’re asking to process > 6 && < 15.
Shouldn’t the second time be > 9 && < 18 to get new ones that weren’t already processed?