如果找不到元素,则重复该函数

时间:2016-12-28 14:43:35

标签: javascript

我试图创建一个脚本,如果有人在Whatsapp网站上内联并且我有这个脚本,它会通知你:

function addZero(i) {
if (i < 10) {
    i = "0" + i;
}
return i;
}

function onlineCheck() {
    var y = document.querySelector('[title="online"]');
    var d = new Date();
    if (y == null) {
      // I want it to repeat onlineCheck() after 1 second
    } else {
        if (y.innerText === 'online') {
            new Notification("contact is online");
            console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) 
            + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) 
            + " " + "Notification sent");
        }
   }
}

我想替换 //我不知道该放什么,其中包含将运行函数的内容onlineCheck()

我应该怎么做

我第一次有这个脚本:

var onlineCheck1 = window.setInterval(function(){ 
    var x = document.querySelector('[title="online"]'); 
    var name = $('#main>header>div.chat-body>div.chat-main>.chat-title>span').text()
    var d = new Date();
    if (x == null) {
        console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) 
        + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) 
        + " " + name + " " + "was" + " " + "offline");
    } else {
        if (x.innerText === "online") {
            console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) 
            + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) 
            + " " + name + " " + "was" + " " + "////online///");
    } else {
        console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) 
        + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) 
        + " " + name + " " + "was" + " " + "offline");
    }
} ,1000);

但是我希望它不会对控制台日志做任何事情我只是希望它重复自己,直到它找到标题=&#34;在线&#34;

的元素

注意:我使用chrome的控制台来运行脚本,如果需要,您可以自己尝试。

4 个答案:

答案 0 :(得分:3)

调用函数本身。这称为递归。您必须小心谨慎地阻止onlineCheck()调用

function onlineCheck() 
{
    var y = document.querySelector('[title="online"]');
    var d = new Date();

    if (y == null) 
    {
        onlineCheck();
    } 
    else 
    {
        if (y.innerText === 'online') 
        {
            new Notification("contact is online");
            console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) + " " + "Notification sent");
        }
    }
}

答案 1 :(得分:0)

您只需按以下方式调用该功能

即可
QStyle::CE_ComboBoxLabel

答案 2 :(得分:-1)

您需要recursion技术。

使用此技术时,recursive function应该需要termination condition来结束进程(否则进程将无限迭代,最终会导致Maximum call stack size exceeded错误)

function onlineCheck() {
var y = document.querySelector('[title="online"]');
var d = new Date();
if (y == null) {
  // Be sure to add a termination condition
  onlineCheck()
} else {
  if (y.innerText === 'online') {
   new Notification("contact is online");
   console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) + " " + "Notification sent");
}
}
}

答案 3 :(得分:-1)

正如其他人所指出的那样,您可以直接调用该函数:onlineCheck();。但是,如果你这样做,循环条件(document.querySelector('[title="online"]') != null)就不会改变,所以它会进入无限循环/递归。

这是另一种方式:

function onlineCheck() {
    var y = document.querySelector('[title="online"]');
    var d = new Date();
    if (y == null) {
        setTimeout(onlineCheck, 10);
    } else {
        if (y.innerText === 'online') {
            new Notification("contact is online");
            console.log(d.toLocaleDateString() + "|" + addZero(d.getHours()) + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds()) + " " + "Notification sent");
        }
    }
}

重新安排在10毫秒后调用onlineCheck,使页面有机会在再次检查之前更新。您可能希望将此数字增加到1000;通过这种方式,它每秒都会检查一次,而不是在适当的位置旋转,去#34; 我们已经完成了吗?现在怎么样?现在怎么样?现在?现在? ?现在&#34;