Javascript在另一个函数中运行一次函数,该函数设置为间隔

Javascript在另一个函数中运行一次函数,该函数设置为间隔,javascript,selenium,selenium-chromedriver,Javascript,Selenium,Selenium Chromedriver,嗨,我在js中使用selenium chrome驱动程序。我每2秒钟检查一次网站上的值。这是我的密码。值每2秒检查一次。它有两个价值。比如在线或离线。我想写入文本文件以更改状态。不是每一张支票。这是我的密码。它正在工作,但它每两秒钟保存一次文本文件。我希望每两秒钟检查一次,但我希望将其保存为纯文本状态更改。请帮帮我 setInterval(MyControl, 2000); function MyControl() { var x = browser.findElements(By.cla

嗨,我在js中使用selenium chrome驱动程序。我每2秒钟检查一次网站上的值。这是我的密码。值每2秒检查一次。它有两个价值。比如在线或离线。我想写入文本文件以更改状态。不是每一张支票。这是我的密码。它正在工作,但它每两秒钟保存一次文本文件。我希望每两秒钟检查一次,但我希望将其保存为纯文本状态更改。请帮帮我

setInterval(MyControl, 2000);

function MyControl() {
  var x = browser.findElements(By.className("textclass")).then(function(divs) {
    // console.log("yy:",divs.length);
    var d = new Date();
    if (divs.length == 1) {
      divs.forEach(function(element) {
        element.getAttribute("title").then(function(text) {
          console.log(text, " Control Time :", d.toLocaleString());
          //    playSound();

          fs.appendFile('mytextfile.txt', text + " Control Time: " + d.toLocaleString() + '\n', function(err) {
            // console.log("/////////////////////////////////////");
            if (err) throw err;
          });
        });
      });
    } else {
      console.log("There isnt any info :" + "Control Time :" + d.toLocaleString());

    }
  });
}

只需根据当前值与以前的值是否不同来设置写入文件的条件

setInterval(MyControl, 2000);

let previousText = null;

function MyControl() {
  var x = browser.findElements(By.className("textclass")).then(function (divs) {
    // console.log("yy:",divs.length);
    var d = new Date();
    if (divs.length == 1) {
      divs.forEach(function (element) {
        element.getAttribute("title").then(function (text) {
          console.log(text, " Control Time :", d.toLocaleString());
          //    playSound();
          if (text != previousText)
            fs.appendFile('mytextfile.txt', text + " Control Time: " + d.toLocaleString() + '\n', function (err) {
              // console.log("/////////////////////////////////////");
              if (err) throw err;
            });
          previousText = text;
        });
      });
    } else {
      console.log("There isnt any info :" + "Control Time :" + d.toLocaleString());

    }
  });
}
还要注意的是,用正确的JS风格重写的上述内容更具可读性:

setInterval(MyControl, 2000);

let previousText = null;

function MyControl() {
  browser.findElements(By.className('textclass')).then(divs => {
    if (divs.length !== 1)
      return;
    let date = new Date();
    div[0].getAttribute('title').then(text => {
      if (text !== previousText)
        fs.appendFile('mytextfile.txt', `${text} Control Time: ${date.toLocaleString()}\n`, err => {
          if (err) throw err;
        });
      previousText = text;
    });
  });
}

只需根据当前值与以前的值是否不同来设置写入文件的条件

setInterval(MyControl, 2000);

let previousText = null;

function MyControl() {
  var x = browser.findElements(By.className("textclass")).then(function (divs) {
    // console.log("yy:",divs.length);
    var d = new Date();
    if (divs.length == 1) {
      divs.forEach(function (element) {
        element.getAttribute("title").then(function (text) {
          console.log(text, " Control Time :", d.toLocaleString());
          //    playSound();
          if (text != previousText)
            fs.appendFile('mytextfile.txt', text + " Control Time: " + d.toLocaleString() + '\n', function (err) {
              // console.log("/////////////////////////////////////");
              if (err) throw err;
            });
          previousText = text;
        });
      });
    } else {
      console.log("There isnt any info :" + "Control Time :" + d.toLocaleString());

    }
  });
}
还要注意的是,用正确的JS风格重写的上述内容更具可读性:

setInterval(MyControl, 2000);

let previousText = null;

function MyControl() {
  browser.findElements(By.className('textclass')).then(divs => {
    if (divs.length !== 1)
      return;
    let date = new Date();
    div[0].getAttribute('title').then(text => {
      if (text !== previousText)
        fs.appendFile('mytextfile.txt', `${text} Control Time: ${date.toLocaleString()}\n`, err => {
          if (err) throw err;
        });
      previousText = text;
    });
  });
}