如何在 if else 语句期间添加暂停/延迟

How to add a pause/delay during if else statements

本文关键字:添加 暂停 延迟 语句 if else      更新时间:2023-09-26

我想放慢我的半自动JavaScript代码。目前,它以每秒约15次的速度一次提取100个列表的详细信息。这在某些方面很好,但会导致跟踪数组中哪个元素与哪个列表相关的问题。这将很难解释,所以请耐心等待,我会尽力而为。

该代码在网页上的每个项目旁边显示一个按钮。 按下第一个按钮时,它会收集与列表相关的详细数据,然后以 0.#### 的形式在按钮内显示文本,然后移动到下一个按钮。

现在可能是我的编码很差导致了问题(原谅我,我很新),但是当值低于 0.006 时,它会做一些事情(在 snippit 中解释),然后执行辅助 .click。此辅助单击大约需要 4 秒来处理,因此我希望代码在移动到列表中的下一项之前等待此操作完成(截图中的最后一段代码执行此操作)。

我已经在截图中做了笔记,希望它很清楚!

// Code excerpt, assume all vars are set here along with the onclick to call this snippit
// If below 0.0010 it will reduce var 'message' to 4 points after decimal, add 'var2' 
// (marked irrelevant) text onto the 'message' variable then click .element2 based off 
// of the contents of 'var1'
// As an example, 'message' is 0.0006. add var2 (great) you have a button showing 
// [great 0.0006]. Then it will interact with the button beside it [.element2]
    	   if(message < 0.0010)    {  
              message = message.toFixed(4);
              message = irrelevant var2 + message;
              $("#" + irrelevant var + " .element1 span").text(message);
           document.getElementsByClassName("element2")[var1].click(); 
            }
// If below 0.006 it will reduce var 'message' to 4 points after decimal, add 'var3' 
// (marked irrelevant) text onto the 'message' variable then click .element2 based off 
// of the contents of 'var1', similar to the first if statement
            else if(message < 0.006)    {  
              message = message.toFixed(4);
              message = irrelevant var3 + message;
              $("#" + irrelevant var + " .element1 span").text(message);
           document.getElementsByClassName("element2")[var1].click(); 
            }
// If below 0.008 it will reduce var 'message' to 4 points after decimal, add 'var4' 
// (marked irrelevant) text onto the 'message' variable then +1 to 'var1'
// This range doesn't interact with another button, it adds 1 to var1 that tells it to
// skip this instance of the .element2 button so it doesn't click the wrong line
           else if(message < 0.008)    {  
              message = message.toFixed(4);
              message = irrelevant var4 + message;
                function var1Count (){
                var1++;}
                var1Count();
              $("#" + irrelevant var + " .element1 span").text(message);
            }
// If greater than 0.008 it will reduce var 'message' to 3 points after decimal, then +1 
// to 'var1'
// This range doesn't interact with another button, it adds 1 to var1 that tells it to
// skip this instance of the .element2 button so it doesn't click the wrong line
           else if(message > 0.008)    {
              message = message.toFixed(3);  
                function var1Count (){
                var1++;}
                var1Count();
            $("#" + irrelevant var + " .element1 span").text(message); 
            }
            
 function testCount (){
                count++;
            }
            testCount();
// This starts the process again but on the next item in the list using the variable count           
	    $(".myButton").click (something);
           document.getElementsByClassName("myButton")[count].click();

对于这种自动化,像硒这样的东西很好,因为自动同步和sleep()方法。

在纯 javascript 中,你想要的是 setTimeout 函数。

查看您的代码,根本不明显为什么需要此超时。如果没有更完整的示例,就很难提供更多帮助。