JavaScript的:我怎样才能从调用它停止后特定时间的函数

问题描述 投票:-2回答:3

只是我想从调用它停止后像5秒特定时间的函数。

我无法找到一个方法来做到这一点,可以有人帮助我

function popUp() {
    // Do some Thing 
    });
popUp();
// how to stop popUp() after calling it after 5 seconds from calling it??
javascript function dom time
3个回答
1
投票

您可以使用setTimout设定的时间量之后运行的功能。例如:

setTimeout(hidePopup, 5000); 

将在5秒钟(5000毫秒)后运行以下功能:

function hidePopup() {
  // Do the opposite
}

0
投票

只需使用return语句:

function popUp() {
    // Do some Thing 
    //Timer simulator
for (i = 0; i < 100; i++) { 
    if (i == 99) return
           }
    });

0
投票

怎么样,你打电话给你的函数,然后在5秒后叫你的setTimeout函数内部改变显示为“无”,就像这样:

function popUp() {
  // Do some Thing

  // SetTimeout to change display to none
  setTimeout(function () {
    document.getElementById('div-id').style.display = "none";
  }, 5000);   
});
popUp();
© www.soinside.com 2019 - 2024. All rights reserved.