JavaScript 计时问题

发布于 2024-07-23 23:08:02 字数 39 浏览 4 评论 0原文

我不会在一定时间内运行一段代码,然后在完成后继续运行另一段代码。

I wont to run a block of code in a certain amount of time and then when done, carry on with another block of code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦幻之岛 2024-07-30 23:08:02
<script type="text/javascript">

var timer = setInterval("firstFunction()","1000"); //every second call firstFunction()
var i = 0;

function firstFunction()
{

    //first code

    i++;
    if(i == 3)
    {
        clearInterval(timer);
        secondFunction();
    }
}

function secondFunction()
{
    //second code
    alert("done!");
}

</script>
<script type="text/javascript">

var timer = setInterval("firstFunction()","1000"); //every second call firstFunction()
var i = 0;

function firstFunction()
{

    //first code

    i++;
    if(i == 3)
    {
        clearInterval(timer);
        secondFunction();
    }
}

function secondFunction()
{
    //second code
    alert("done!");
}

</script>
甜柠檬 2024-07-30 23:08:02

使用 setTimeout() 可能就是您想要的。 例如...

<script type="text/javascript">
    function YourFunction()
    {
        alert('Hello Stackoverflow');
    }

    window.setTimeout(YourFunction, 1000);
</script>

希望有帮助

Using the setTimeout() is probably what you want. For example...

<script type="text/javascript">
    function YourFunction()
    {
        alert('Hello Stackoverflow');
    }

    window.setTimeout(YourFunction, 1000);
</script>

Hope it helps

北风几吹夏 2024-07-30 23:08:02

这就是您的做法,使用 setTimeout 函数,它将要调用的代码作为第一个参数,以及调用它之前应等待的时间(以毫秒为单位)作为第二个参数:

function callWhenDone() {
    // code to call when timeout finishes
}

setTimeout(function() {
    // initial code to run
    callWhenDone();
}, 5000); // 5000 = run in 5 seconds

由于 Javascript 的性质,您必须封装要运行的代码在其自己的函数中超时完成后,否则将在超时完成之前运行。 从本质上讲,这是一个回调,它是Javascript 基于事件的性质。

This is how you would do it, using the setTimeout function, which takes code to call as the first argument and how much time it should wait before calling it (in milliseconds) as the second argument:

function callWhenDone() {
    // code to call when timeout finishes
}

setTimeout(function() {
    // initial code to run
    callWhenDone();
}, 5000); // 5000 = run in 5 seconds

Because of the nature of Javascript you have to encapsulate the code you want to run after the timeout is finished in its own function, otherwise it would be run before the timeout is finished. This is, in essense, a callback, and it is a big part of the event-based nature of Javascript.

错々过的事 2024-07-30 23:08:02

您需要使用 setTimeout() 函数。

You'll want to use the setTimeout() function.

谜兔 2024-07-30 23:08:02

setTimeout - 在一段时间间隔后执行代码

clearTimeout - 取消 setTimeout()

更多详细信息 此处

setTimeout - executes code after a time interval

clearTimeout - cancels the setTimeout()

More details here.

书信已泛黄 2024-07-30 23:08:02

使用setTimeout。

setTimeout(function() {
    // code here
    // you can use it recursively
    //   setTimeout(...);
  },
  1000 // 1000 miliseconds (= 1 second)
);

setIntervalsetTimeout 类似,只不过它重复执行代码。

Use setTimeout.

setTimeout(function() {
    // code here
    // you can use it recursively
    //   setTimeout(...);
  },
  1000 // 1000 miliseconds (= 1 second)
);

and setInterval is like setTimeout, except it repeats a code repeatedly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文