简体   繁体   English

如何在我的 function 中嵌套 setInterval 和 setTimeout?

[英]How can I nest setInterval and setTimeout in my function?

Good Morning,早上好,

I have a javascript function which, in turn, calls several functions.我有一个 javascript function,它又调用了几个函数。 I would like to trigger the function (check1 in my example below) half a second after the javascript function is called and subsequently call the same function check1 every 10 seconds (10000 milliseconds).我想在调用 javascript function 后半秒触发 function(在我的示例中为 check1),随后每 10 秒(10000 毫秒)调用相同的 function check1。 Having done some researches, I found out - I might be wrong though - that the combination of setInterval and setTimeout would have made the deal.做了一些研究后,我发现——虽然我可能是错的——setInterval 和 setTimeout 的组合会达成协议。

Unfortunately, the outcome was not as expected.不幸的是,结果并不如预期。

Afterwards, I made a second trial by using the clearInterval() but I was not sure either how to nest these various functions.之后,我使用 clearInterval() 进行了第二次尝试,但我也不确定如何嵌套这些不同的函数。

Is there an elegant and smart way to achieve this?有没有一种优雅而聪明的方法来实现这一目标? Many thanks in advance for your help非常感谢您的帮助

Here below is my javascript code:下面是我的 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;
    }
  }
  });
}

Never use setInterval with async/ajax永远不要将 setInterval 与 async/ajax 一起使用

Just use the setTimeout只需使用 setTimeout

OR move或移动

setTimeout(check1, 10000); setTimeout(check1, 10000);

to your special function给你特别的 function

// Global variables
var AutoScript = false;

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;
      setTimeout(check1, 10000);
    }
  });
}
$(function() {
  check1()
})

The problem is you're calling check1 in an interval but not clearing it.问题是你在一个时间间隔内调用check1但没有清除它。 So this will keep getting called every 500 miliseconds.所以这将继续每500毫秒被调用一次。

setInterval(check1, IntervalRefresh1);

On the top, you're calling the function again with setTimeout .在顶部,您使用setTimeout再次调用 function。 You must clear that too at some point based on a condition so that it doesn't run infinitely.你也必须根据条件在某个时候清除它,这样它就不会无限运行。 See the code and comments.查看代码和注释。

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;
    }
  }
  });

Good Morning,早上好,

I have a javascript function which, in turn, calls several functions.我有一个 javascript function ,它又调用了几个函数。 I would like to trigger the function (check1 in my example below) half a second after the javascript function is called and subsequently call the same function check1 every 10 seconds (10000 milliseconds). I would like to trigger the function (check1 in my example below) half a second after the javascript function is called and subsequently call the same function check1 every 10 seconds (10000 milliseconds). Having done some researches, I found out - I might be wrong though - that the combination of setInterval and setTimeout would have made the deal.做了一些研究后,我发现——虽然我可能错了—— setInterval 和 setTimeout 的组合会达成交易。

Unfortunately, the outcome was not as expected.不幸的是,结果并不像预期的那样。

Afterwards, I made a second trial by using the clearInterval() but I was not sure either how to nest these various functions.之后,我使用 clearInterval() 进行了第二次尝试,但我不确定如何嵌套这些不同的函数。

Is there an elegant and smart way to achieve this?有没有一种优雅而聪明的方法来实现这一目标? Many thanks in advance for your help非常感谢您的帮助

Here below is my javascript code:下面是我的 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;
    }
  }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM