首页 > 解决方案 > 具有 HTML/JQuery 可见性的 Javascript 循环

问题描述

我正在尝试创建一个循环,在继续之前检查一些事情。如果循环不是实现这一点的最佳方式,我对新想法持开放态度。

请注意:如果措辞非常糟糕,我很抱歉,这是匆忙的。

更新:

让我们用实际的例子来重新表述这一点。我是通过片段(Chrome)来做到这一点的,所以这不是网站内置的,我也不拥有网站,我只是在创建一个工具,让这对我来说更容易。

而不是行,我们称它们为“任务”。当我打开一个任务时,$('#workRequestDetailsPanel')变得可见。一旦可见,我想通过运行如下所示的命令来提交任务app.workRequestDetails.SubmitWorkRequestDetails()。运行此函数时,它会关闭允许打开新任务的任务。如果我在一个已经打开的任务中打开一个新任务,它会崩溃并需要我重新加载网站,所以我需要等待整个过程结束才能打开一个新任务。

这个例子有点工作,但使用的是网站非常不擅长的时间。例如,有时关闭任务需要超过 4 秒的时间。

var checkExist = setInterval(function() {
      if ($('#workRequestDetailsPanel').is(':visible')) {
         app.workRequestDetails.SubmitWorkRequestDetails();
         app.modalPopup.ClosePopup();
         setTimeout(function(){
            if($('#workRequestDetailsPanel').length == 0){
                  app.dashboardGridview.OpenPopup($('.GVSingleCheckbox').eq(1).attr('id'));
            }else{
                  app.workRequestDetails.CloseWorkrequestDetailsPopup();
            }
         }, 4000);
      }
}, 4000);

标签: javascriptjqueryhtml

解决方案


像这样的东西,浮现在脑海中:

// Check our limit, so that we don't end up with an infinite loop. 
// You should replace this with the length of the available table rows
let numOfRows = 10; 

// Start from the first row
let currentRow = 1;

// Rows are laballed like this: #row_1, #row_2, ... #row_n
function checkRow( rowNum ){

    let checkExist = setInterval(function() {
        if ( $('#row_' + rowNum ).is(':visible') ) { //once visible, close the "row"
           close( rowNum ); // Close current row
           currentRow++; // Increment the currentRow
           clearInterval( checkExist );
           // If we have more rows to check, run the function again using the incremented number
           if ( currentRow <= numOfRows ){
               checkRow( currentRow );
           }
        }
      }, 100); // every tick check if the element is visible (this signifies if the "row" is open)

}

checkRow( currentRow ); // Start checking row #1. Recursion will take care of the rest.

我不确定该close()函数究竟做了什么以及行如何变得可见,但这段代码可能是您尝试实现的一个很好的起点。


推荐阅读