简体   繁体   中英

Wait for a few seconds before executing next line of code?

The purpose of this code is to retrieve the URLs of search results. The website I am working with doesn't load all of the results unless you scroll the entire page. However, it takes a few seconds after scrolling for all of the results to load, and as it is, the next line is executed right away and it only retrieves the first few links instead of the entire page.

I think all I need for this to work is just a pause for a few seconds.

The xpath in this example is for google which doesn't lazy load, the site I'm using is behind a login and it does lazy load.

window.scrollTo({ top:document.body.scrollHeight, behavior: 'smooth', })

///pause here

try {

  var maxLinks = 25;  
  var returnData = "URL";  
  var xPath = '//*[@class="r"]/a';

  var xpathResults = document.evaluate(xPath, document, null, 0, null);

  var oNode = xpathResults.iterateNext();

  var nodeList = [];
  var linkCount = 0;
  var hrefStr;
  var returnStr;
  var linkText;

  while (oNode && (linkCount < maxLinks)) {

    if (oNode.href !== hrefStr) {
      linkCount += 1;
      hrefStr = oNode.href;
      linkText = oNode.textContent;

      if (returnData === "MD") {
        returnStr = "[" + linkText + "](" + hrefStr + ")";
      }
      else {
        returnStr = hrefStr;
      }

      nodeList.push(returnStr);
    }

    oNode = xpathResults.iterateNext();

  } 

  returnResults = nodeList.join('\n');


} catch (pError) {

    if (!oError.message) {
      oError.message = pError.toString();
    }

    oError.message = "[ERROR]"
      + "\n\nError Number: " + oError.errorNumber + "\n"
      + oError.message

    returnResults = oError.message;  
} 

function copyToClipboard(text) {
    var dummy = document.createElement("textarea");
    document.body.appendChild(dummy);
    dummy.value = text;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}
copyToClipboard(returnResults)
copyToClipboard(returnResults)

There is sleep method solution that allows you to wait time between actions as I mentioned in my comment earlier.

Here is my solution:

    function sleep (time) { //Sleep function
      return new Promise((resolve) => setTimeout(resolve, time));
    }
    sleep(500).then(() => { //Wait the stated time then do something..
         var maxLinks = 25;  
  var returnData = "URL";  
  var xPath = '//*[@class="r"]/a';

  var xpathResults = document.evaluate(xPath, document, null, 0, null);

  var oNode = xpathResults.iterateNext();

  var nodeList = [];
  var linkCount = 0;
  var hrefStr;
  var returnStr;
  var linkText;

  while (oNode && (linkCount < maxLinks)) {

if (oNode.href !== hrefStr) {
  linkCount += 1;
  hrefStr = oNode.href;
  linkText = oNode.textContent;

  if (returnData === "MD") {
    returnStr = "[" + linkText + "](" + hrefStr + ")";
  }
  else {
    returnStr = hrefStr;
  }

  nodeList.push(returnStr);
}

oNode = xpathResults.iterateNext();

  } 

  returnResults = nodeList.join('\n');


} catch (pError) {

    if (!oError.message) {
      oError.message = pError.toString();
    }

    oError.message = "[ERROR]"
      + "\n\nError Number: " + oError.errorNumber + "\n"
      + oError.message

    returnResults = oError.message;  
} 

function copyToClipboard(text) {
    var dummy = document.createElement("textarea");
    document.body.appendChild(dummy);
    dummy.value = text;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}
copyToClipboard(returnResults)
copyToClipboard(returnResults)
});
   }

There isn't a sleep() in the same way that there is in Java. If you don't understand why, you'll need to read about threading (or the lack thereof) in JavaScript. However, you can create an async function and then call it with an await (but your code will need to be in an async function:

function sleep(millis) {
  return new Promise(resolve => setTimeout(resolve, millis));
}


async function doIt() {
    const max = 4;
    let count = 0;
    while (count < max) {
        await sleep(1000);
        console.log('loop: %s', count);
        count++;
    }
}

doIt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM