function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gokubigokubi 

Looping through an array and refreshing the browser

I'm trying to convert an array of Leads and refresh the browser after each Lead is converted. I've got it all working except for the refresh--currently the browser doesn't refresh until after all the leads have been converted. Here's what I've got.

Code:
function createLeadToConvert(LeadArray,startRow) {
  var row = startRow;
  if (row<LeadArray.length){
    ThisLeadId = LeadArray[row];
    //lots of conversion code here
 //here I change a part of the page
document.getElementById("Row" + leadId + "Lead").innerHTML = "Converted"; //increment the row I'm looking at
startRow++;
//make the recursive call, hitting the next row of the LeadArray
 setTimeout(createLeadToConvert(LeadArray,startRow),50); } }
The setTimeout call isn't quoted, so I know that it will fire without the 50 milisecond pause. But I can't seem to pass the LeadArray in when it's quoted. I can pass the startRow, because it's just a number, but I'm unsuccessful at passing the array.

Any thoughts on how I could get the page to refresh after processing each row?

Thanks!
 

DevAngelDevAngel
Code:
var leadStack = [];
function getLeads() {
    //run a query or something
    leadStack = queryResult.getArray("records");
    window.setTimeout("createLeadToConvert()", 50);
}
function createLeadToConvert() {
  var lead = leadStack.pop();
    //lots of conversion code here    //here I change a part of the page    document.getElementById("Row" + leadId + "Lead").innerHTML = "Converted";
    //increment the row I'm looking at    startRow++;    //make the recursive call, hitting the next row of the LeadArray    setTimeout(createLeadToConvert(LeadArray,startRow),50);
  }
  if (leadStack.length > 0) {
    window.setTimeout("createLeadToConvert()", 50);
  } else {
    //remove the progress bar or something
  }
}

 
Hi gokubi,
 
I think what you want to do is something closer to a stack.  You put your array of leads is a globally scoped variable.  You then call the conversion function from setWindowTimeout.  When you get into your conversion function, "pop" a lead off of the stack.  At the end of the conversion function you can check to see if you still have leads in the stack, and if so, call the conversion function again from the setWindowTimeout.  When the stack is empty, you are done.
 
 
gokubigokubi
Thanks Dave, that is indeed what I did to solve the problem once I got some offline advice from Evan Callahan along that course.

Steve