I'm trying to write a Greasemonkey script where different pages are loaded with .load() using a for loop and store some of their informations in localStorage.
My Problem: because .load() is asynchronous it doesn't work well. My script just stores the Last value I am trying to store. I think that is because i use the var i inside the callback function, and when the callback function is fired i has already another value.
for (var i = 0; i < plantsDiv.length; i++)
{
objectid = plantsDiv[i].getAttribute('objectid');
$("#wrap").load("index.php?objectid="+ objectid,function(){
//[...] what is happening
window.localStorage.setItem(objectid,value);
});
}
When i insert alert("foo") between the callback function and the end of the for loop everything works, because the .load function is loaded, while i am clicking "ok" in the alert window.
Is there any way to let the for loop wait until the whole .load is executed?
Thanks for your help, and sorry for my Bad english :)
varbeforeobjectid = plantsDiv[i].getAttribute('objectid');ithat's the issue. Don't declare it outside the loop - declare it inside ;)valuecome from?