Javascript 如何在函数中嵌套setInterval和setTimeout?

Javascript 如何在函数中嵌套setInterval和setTimeout?,javascript,settimeout,setinterval,Javascript,Settimeout,Setinterval,早上好 我有一个javascript函数,它依次调用多个函数。 我希望在调用javascript函数半秒后触发函数(下面的示例中为check1),然后每隔10秒(10000毫秒)调用同一个函数check1。在做了一些研究之后,我发现——尽管我可能错了——setInterval和setTimeout的组合会达成协议 不幸的是,结果并不像预期的那样 之后,我使用clearInterval()进行了第二次尝试,但我也不确定如何嵌套这些不同的函数 有没有一个优雅而聪明的方法来实现这一点?非常感谢你的帮助

早上好

我有一个javascript函数,它依次调用多个函数。 我希望在调用javascript函数半秒后触发函数(下面的示例中为check1),然后每隔10秒(10000毫秒)调用同一个函数check1。在做了一些研究之后,我发现——尽管我可能错了——setInterval和setTimeout的组合会达成协议

不幸的是,结果并不像预期的那样

之后,我使用clearInterval()进行了第二次尝试,但我也不确定如何嵌套这些不同的函数

有没有一个优雅而聪明的方法来实现这一点?非常感谢你的帮助

下面是我的javascript代码:

// Global variables
var AutoScript = false;
var IntervalRefresh1 = 500;
var newIntervalDefined1 = false;

// calls the startUp method
startUp();


function startUp()
{
  console.log("I'm the startup2");
  setInterval(check1, IntervalRefresh1);
}


function check1()
{
  $.ajax({
  type: 'POST',
  url: 'php/checker1.php',
  dataType: 'json',
  data: {
  counter:$('#message-list').data('counter')
  }
  }).done(function( response ) {
  /* check if with response we got a new update */
  if(response.update==true)
  {
    var j = response.news;      
    $('#message-list').html(response.news);
    AutoScript = true;
    // here I call a specific method named after getDataFromDatabase(j);
    AutoScript = false;        
    if (newIntervalDefined1 == false)
    {
      setTimeout(check1, 10000);
      newIntervalDefined1 == true;
    }
  }
  });
}

切勿将setInterval与异步/ajax一起使用

只需使用setTimeout

或移动

设置超时(检查1,10000)

为了你的特殊功能

//全局变量
var AutoScript=false;
函数检查1(){
$.ajax({
键入:“POST”,
url:'php/checker1.php',
数据类型:“json”,
数据:{
计数器:$('#消息列表')。数据('计数器')
}
}).完成(功能(响应){
/*检查我们是否有新的更新*/
如果(response.update==true){
var j=response.news;
$('#消息列表').html(response.news);
AutoScript=true;
//这里我调用一个以getDataFromDatabase(j)命名的特定方法;
AutoScript=false;
设置超时(检查1,10000);
}
});
}
$(函数(){
检查1()
})

切勿将setInterval与async/ajax一起使用

只需使用setTimeout

或移动

设置超时(检查1,10000)

为了你的特殊功能

//全局变量
var AutoScript=false;
函数检查1(){
$.ajax({
键入:“POST”,
url:'php/checker1.php',
数据类型:“json”,
数据:{
计数器:$('#消息列表')。数据('计数器')
}
}).完成(功能(响应){
/*检查我们是否有新的更新*/
如果(response.update==true){
var j=response.news;
$('#消息列表').html(response.news);
AutoScript=true;
//这里我调用一个以getDataFromDatabase(j)命名的特定方法;
AutoScript=false;
设置超时(检查1,10000);
}
});
}
$(函数(){
检查1()
})

问题是您在一段时间间隔内调用了
check1
,但没有清除它。因此,它将每隔
500
毫秒被调用一次

setInterval(检查1,IntervalRefresh1)

在顶部,使用
setTimeout
再次调用函数。你也必须在某个时刻根据条件清除它,这样它就不会无限运行。请参阅代码和注释

var timerinterval;
var timeout;
function startUp()
{
  console.log("I'm the startup2");
  timerinterval = window.setInterval(check1, IntervalRefresh1); //set a timerInterval
}


function check1()
{
  $.ajax({
  type: 'POST',
  url: 'php/checker1.php',
  dataType: 'json',
  data: {
  counter:$('#message-list').data('counter')
  }
  }).done(function( response ) {
  /* check if with response we got a new update */
  if(response.update==true)
  {
    var j = response.news;      
    $('#message-list').html(response.news);
    AutoScript = true;
    // here I call a specific method named after getDataFromDatabase(j);
    AutoScript = false;        
    if (newIntervalDefined1 == false)
    {
      clearInterval(timerinterval); //clear the interval timer
     timeout =  setTimeout(check1, 10000); //now clear this too at some point of time so that it doesn run infinitely
      newIntervalDefined1 == true;
    }
  }
  });

问题是您在一段时间内调用了
check1
,但没有清除它。因此,它将每隔
500
毫秒被调用一次

setInterval(检查1,IntervalRefresh1)

在顶部,使用
setTimeout
再次调用函数。你也必须在某个时刻根据条件清除它,这样它就不会无限运行。请参阅代码和注释

var timerinterval;
var timeout;
function startUp()
{
  console.log("I'm the startup2");
  timerinterval = window.setInterval(check1, IntervalRefresh1); //set a timerInterval
}


function check1()
{
  $.ajax({
  type: 'POST',
  url: 'php/checker1.php',
  dataType: 'json',
  data: {
  counter:$('#message-list').data('counter')
  }
  }).done(function( response ) {
  /* check if with response we got a new update */
  if(response.update==true)
  {
    var j = response.news;      
    $('#message-list').html(response.news);
    AutoScript = true;
    // here I call a specific method named after getDataFromDatabase(j);
    AutoScript = false;        
    if (newIntervalDefined1 == false)
    {
      clearInterval(timerinterval); //clear the interval timer
     timeout =  setTimeout(check1, 10000); //now clear this too at some point of time so that it doesn run infinitely
      newIntervalDefined1 == true;
    }
  }
  });

非常感谢你,ABGR。我不明白的是,如果我想无限期每隔10秒运行一次check1,为什么我也要清除timeout?或者我遗漏了一点。如果你想无限地运行它,你不必清除它。但通常你想在某个时候停止。但是,如果你有意允许,你可以这样做。非常感谢你,ABGR。我不明白的是,如果我想无限期每隔10秒运行一次check1,为什么我也要清除timeout?或者我遗漏了一点。如果你想无限地运行它,你不必清除它。但通常你想在某个时候停止。但是,如果您有意允许,您可以这样做。