Try to search your question here, if you can't find : Ask Any Question Now ?

Wait till the another funciton is complete

HomeCategory: stackoverflowWait till the another funciton is complete
kundan asked 1 week ago

I am trying to feed an iput box with a list of values using a for loop. i’m trying to do the following.
1. i have an array of values.
2. i want to use each of these values to search a web page.
3. i want to pause the loop till the search is completed with one value.

i tried the following code:


var searchArray = ["260222", "216043", "260794"];

async function letsStart() {
  if (document.readyState === "complete") {
    for (k = 0; k < searchArray.length; k++) {
      var iValue = document.getElementById("fileNumber");
      iValue.value = searchArray[k];
      console.log("St1");
      var iSearch = document.getElementsByClassName("btn_blue");
      console.log("St2");
      var searchBtn = document.getElementById(iSearch[0].id).click();
      var theResult = await readyToGo();
      console.log("St3");
      console.log(theResult);
    }
  } else {
    setInterval(letsStart, 1000);
  }
};

function readyToGo() {
  if (document.readyState === "complete") {
    return new Promise((resolve, reject) => {
      console.log("Started");
      var sData = document.getElementsByClassName("inboxListTbl");
      console.log(sData.length);
      var tData = document.getElementById(sData[1].id);
      console.log(tData.rows.length);
      if (tData.rows.length > 2) {
        for (i = 0; i < tData.rows.length; i++) {
          for (j = 0; j < tData.rows[i].cells.length; j++) {
            if (tData.rows[i].cells[j].innerText.match("Hello")) {
              var dataNeeded = tData.rows[i].cells[3].innerText + " " + tData.rows[i].cells[j - 1].innerText;
            }
          }
        }
      } else {
        var dataNeeded = tData.rows[1].cells[3].innerText + " " + tData.rows[1].cells[6].innerText;
      } 
      resolve(dataNeeded);
    })
  } else {
    setInterval(readyToGo, 1000);
  }
}

I am unable to pause the for loop. its running three times and at the end search buttion is being clicked with the first values of searchArray.

Following is the console log:

“St1
VM7871:8 St2
VM7871:22 Started
VM7871:24 2
VM7871:26 6
VM7871:11 St3
VM7871:12 260222 File/Created/(Computer No. 366600)/11-33/2019-SDF
PromiseĀ {: undefined}”

i’m getting the same thing three time instead of different resutls.

1 Answers
Best Answer
naveen answered 1 week ago
Your Answer

17 + 6 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com