简体   繁体   English

在 For 循环中检查元素是否存在 SetInterval

[英]Check if element exists SetInterval in For Loop

I'm trying to check if each element has been populated before generated a gauge.我正在尝试检查在生成仪表之前是否已填充每个元素。 The gauge value is being set by another API so having to wait for the value to be present.仪表值由另一个 API 设置,因此必须等待该值出现。

Having the check in the For Loop doesn't seem to work在 For 循环中进行检查似乎不起作用

function refreshGauge() {
var table = document.getElementById("mytab1");
var targetTDs = table.querySelectorAll(".myfindclass2");
for (var i = 0; i < targetTDs.length; i++) {
  var td = targetTDs[i];
  var fvals = td.querySelectorAll("[id*='calcValue']");
  var fval = fvals[0].innerText.replace('%','');
  var checkElem = "#" + fvals[0].getAttribute('id');
  console.log(checkElem)
//HERE Doesn't work 

var checkExist = setInterval(function() {
   if ($(checkElem).length) {
      console.log("Exists!");
         nextstep(td)
      clearInterval(checkExist);
  }
}, 100); // check every 100ms   


  }
 console.log('done')
} ;


    refreshGauge()

The above results in an infinite loop checking the element only a single element.上面的结果是一个无限循环,只检查一个元素。

If I use the below it works but I don't to list all 36+ in the list:如果我使用下面的它可以工作,但我不会在列表中列出所有 36+:

What is the best way to accomplish having each element checked to ensure the values populated before creating/updating the gauge.完成检查每个元素以确保在创建/更新仪表之前填充值的最佳方法是什么。

function refreshGauge() {
var table = document.getElementById("mytab1");
var targetTDs = table.querySelectorAll(".myfindclass2");
for (var i = 0; i < targetTDs.length; i++) {
  var td = targetTDs[i];
  var fvals = td.querySelectorAll("[id*='calcValue']");
  var fval = fvals[0].innerText.replace('%','');
  var checkElem = "#" + fvals[0].getAttribute('id');
  console.log(checkElem)


nextstep(td)


  }
 console.log('done')
} ;  

  var checkExist = setInterval(function() {
     if ($('gauge1').length && $('gauge2').length && $('gauge3').length  && $('gauge4').length) {
        console.log("Exists!");
         refreshGauge()
        clearInterval(checkExist);
    }
  }, 100); // check every 100ms 

After testing the two codes I learned I should be evaluating the if the value I need is still undefined or has a value.在测试了我了解到的两个代码之后,我应该评估我需要的值是否仍然未定义或具有值。 var fval = fvals[0].innerText.replace('%','');

I'm trying something like this but not working:我正在尝试这样的事情但不工作:

function refreshGauge() {
var table = document.getElementById("mytab1");
var targetTDs = table.querySelectorAll(".myfindclass2");
for (var i = 0; i < targetTDs.length; i++) {
  var td = targetTDs[i];
  var fvals = td.querySelectorAll("[id*='calcValue']");
  var fval = fvals[0].innerText.replace('%','');
  var checkElem = "#" + fvals[0].getAttribute('id');
  console.log(fval )
//HERE Doesn't work 

var checkExist = setInterval(function() {
   if (fval.length) {
      console.log("Exists!");
         nextstep(td)
      clearInterval(checkExist);
  }
}, 100); // check every 100ms   


  }
 console.log('done')
} ;


    refreshGauge()

Seems like extracting a waitForElement function which takes an element selector as an argument and returns a promise resolving the found element might be quite helpful.似乎提取waitForElement function 它将元素选择器作为参数并返回 promise 解析找到的元素可能会很有帮助。

You can loop through the tds, get the selector id and pass it to the waitForElement function and .then nextstep您可以遍历 tds,获取选择器 ID 并将其传递给waitForElement function 和.then nextstep

function refreshGauge() {
    const table = document.getElementById("mytab1");
    const targetTDs = table.querySelectorAll(".myfindclass2");
    targetTDs.forEach(td => {
        const td = targetTDs[i];
        const fvals = td.querySelectorAll("[id*='calcValue']");
        const selector = "#" + fvals[0].getAttribute("id");
        waitForElement(selector)
            .then(() => nextstep(td))
    })
}
function waitForElement(selector) {
    return new Promise((resolve) => {
        const checkExist = setInterval(function () {
            const elements = $(selector)
            if (elements.length) {
                clearInterval(checkExist);
                resolve(elements[0]);
            }
        }, 100);
    })
}

Note arrow functions, const and Promise usage might not be acceptable in all browsers注意箭头函数、const 和 Promise 的使用可能并非在所有浏览器中都可接受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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