javascript - 有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);



时间: 作者:

想到的是:


// 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.



作者:
...