如何在 10 秒时向用户发出警告。刷新setInterval?

How to make a warning to user when 10 secs. to refresh setInterval?

我正在使用 setInterval 来刷新页面中的图形内容。如何在 setinterval 即将刷新 'the content will be refreshed'.

时向用户显示警报

例如,我的间隔使用 300 秒,我想在 290 秒过后显示每个间隔 'The content will be refreshed after 10 secs. Do you want to refresh the graph' 的警报。这该怎么做?有什么建议吗?

类似的东西:

var interval;
var secondsPassed = 0;
function refreshWithMessage() {
  interval = setInterval(function(){
    if(secondsPassed == 290) {
       //showMessage with options
    }
    if(secondsPassed == 300) {
       clearInterval(interval);
       //do what you need here (refresh page?)
    }
    secondsPassed++;
  }, 1000)
}

如果您需要让用户取消间隔 - 您应该清除间隔,然后将秒变量重置为 0。

你应该设置你的时间间隔为 290000ms,然后它会显示一个警报,然后你使用 setTimeout 在 10 秒后更新你的页面,就像这样:

  setInterval(function() {
    alert("10 seconds to update")
    setTimeout(function() {
         //REFRESH PAGE
     },5000);
  }, 10000);

在您的页面加载中,您可以刷新您的 290 规格

 setInterval(function(){ 
     showDialogBox("do you want to refresh");
 }, 290000);

然后在确认按钮上添加一个将延迟最后 10 秒的操作

 confirmButtonOnClick(){
    setInterval(function(){ 
       refresh();
    }, 10000);
 }

在这种情况下,我更喜欢使用 setTimeout 而不是 setInterval,因为它可以提供更好的控制。

所以我设置了两个计时器——一个用于警告,另一个用于超时本身,然后我重新启动这两个计时器。

const timeoutWarning = 1800;
const timeoutDone = 2000;

function showWarning() {
  console.log('About to time out...');
}

function doTimeout() {
  console.log('Timeout reached');
  setWarnings();
}

function setWarnings() {
  setTimeout(showWarning, timeoutWarning);
  setTimeout(doTimeout, timeoutDone);
}

setWarnings();