setInterval 函数不在定时间隔上运行,只是连续重复,没有时间

setInterval function not running on timed Interval, just repeating continuously with no time

本文关键字:连续 没有时间 运行 函数 定时间 setInterval      更新时间:2023-09-26

只是好奇,我有这个功能:

setInterval( function(){
       sendAjax('search', eInput, function(responseText){
              $("#txtResp").html(responseText);
       })
}, 5000 );

它连续运行此函数,没有间隔。它导致我的 sendAjax 函数疯狂运行。

我只需要它每 5 秒运行一次。有什么帮助吗?

问候

泰勒

怀疑这是问题所在,但不要忘记用分号结束你的行

setInterval( function(){
         sendAjax('search', eInput, function(responseText){
                $("#txtResp").html(responseText);
         }); // <---
 }, 5000 );
此外,仅仅

因为您每 5 秒执行一次操作,无论间隔如何,都会在收到响应时执行对 sendAjax 的回调,这可能是一个问题。因此,您可能会看到连续的响应反复写入 #txtResp

div