简体   繁体   English

如何正确使用setInterval和clearInterval

[英]how to use setInterval and clearInterval properly

Here is code that I wrote. 这是我编写的代码。

function formatDate(date) {
  var day;
  if (date.getDate() < 10) {
    day = "0" + date.getDate();
  } else {
    day = date.getDate();
  }

  var month;
  var tempMonth = date.getMonth() + 1;
  if ( tempMonth < 10){
    month = "0" + tempMonth;
  } else {
    month = tempMonth;
  }
  var year = date.getFullYear();

  return year + "-" + month + "-" + day;
}

Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

jobDate = new Date(2016, 3, 7);
endDate = new Date(2016, 4, 1);
expireInterval = false;

timeoutVar = setTimeout(runJobFromJS,1000);

function runJobFromJS(){
  var strDate = formatDate(jobDate);
  console.log(strDate);

  jobDate = jobDate.addDays(1);

  if (jobDate >= endDate){
    clearInterval(timeoutVar);
  }
}

I was expecting that strDate will print dates from April 07 till May 01. But it is only printing it once. 我期望strDate可以打印从4月7日到5月01日的日期。但是它只打印一次。 Why isn't that setInterval not working proper 为什么setInterval不能正常工作

You are using setTimeout (executes a function once after an amount of time in milliseconds) instead of setInterval (continuously executes a function). 您使用的是setTimeout (在以毫秒为单位的时间后执行一次函数),而不是setInterval (连续执行一个函数)。

The documentation for setInterval : setInterval的文档:

The setInterval() method of the WindowOrWorkerGlobalScope mixin repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. WindowOrWorkerGlobalScope mixin的setInterval()方法重复调用函数或执行代码段,每次调用之间有固定的时间延迟。 It returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval(). 它返回一个唯一标识该间隔的间隔ID,因此您以后可以通过调用clearInterval()将其删除。

The documentation for setTimeout : setTimeout的文档:

The setTimeout() method of the WindowOrWorkerGlobalScope mixin (and successor to window.setTimeout) sets a timer which executes a function or specified piece of code once after the timer expires. WindowOrWorkerGlobalScope mixin的setTimeout()方法(和window.setTimeout的后继方法)设置一个计时器,该计时器在计时器到期后执行一次功能或指定的代码。

timeoutVar = setInterval(runJobFromJS,1000);//not setTimeout

function runJobFromJS(){
  var strDate = formatDate(jobDate);
  console.log(strDate);

  jobDate = jobDate.addDays(1);

  if (jobDate >= endDate){
    clearInterval(timeoutVar);
  }
}

Demo: 演示:

 function formatDate(date) { var day; if (date.getDate() < 10) { day = "0" + date.getDate(); } else { day = date.getDate(); } var month; var tempMonth = date.getMonth() + 1; if ( tempMonth < 10){ month = "0" + tempMonth; } else { month = tempMonth; } var year = date.getFullYear(); return year + "-" + month + "-" + day; } Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } jobDate = new Date(2016, 3, 7); endDate = new Date(2016, 4, 1); expireInterval = false; timeoutVar = setInterval(runJobFromJS,1000); function runJobFromJS(){ var strDate = formatDate(jobDate); document.getElementById("result").textContent = strDate; //console.log(strDate); jobDate = jobDate.addDays(1); if (jobDate >= endDate){ clearInterval(timeoutVar); } } 
 <span id="result"></span> 

You're using setTimeOut (execute only once) instead of setInterval() 您正在使用setTimeOut (仅执行一次),而不是setInterval()

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。 The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed. setInterval()方法将继续调用该函数,直到调用 clearInterval()或关闭窗口为止。

-- -

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. setTimeout()方法将在指定的毫秒数后调用一个函数或对一个表达式求值。 Tip: The function is only executed once. 提示: 该功能仅执行一次。 If you need to repeat execution, use the setInterval() method. 如果需要重复执行,请使用setInterval()方法。

In your case, try: 对于您的情况,请尝试:

 function formatDate(date) { var day; if (date.getDate() < 10) { day = "0" + date.getDate(); } else { day = date.getDate(); } var month; var tempMonth = date.getMonth() + 1; if ( tempMonth < 10){ month = "0" + tempMonth; } else { month = tempMonth; } var year = date.getFullYear(); return year + "-" + month + "-" + day; } Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } jobDate = new Date(2016, 3, 7); endDate = new Date(2016, 4, 1); expireInterval = false; timeoutVar = setInterval(runJobFromJS,1000); //here! function runJobFromJS(){ var strDate = formatDate(jobDate); console.log(strDate); jobDate = jobDate.addDays(1); if (jobDate >= endDate){ clearInterval(timeoutVar); } } 

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

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