setInterval(function(),time)在运行时更改时间 [英] setInterval(function(),time) change time on runtime

查看:141
本文介绍了setInterval(function(),time)在运行时更改时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在代码运行时更改setInterval函数的时间.

I want to change setInterval function time when my code is running.

我尝试

<script type="text/javascript">
        $(function () {
            var timer;
            function come() { alert("here"); }
            timer = setInterval(come, 0);
            clearInterval(timer);
            timer = setInterval(come, 10000);
        });
    </script>

第一个SetInterval不起作用!

First SetInterval does not work!

推荐答案

您正在清除下一行的间隔,因此第一个间隔将不起作用,因为它会立即被清除:

You're clearing the interval on the next line, so the first one wont work, as it gets cleared right away :

        timer = setInterval(come, 0);
        clearInterval(timer);
        timer = setInterval(come, 10000);

而且,正如gdoron所说,设置无间隔时间并不是真正有效,也不是一个好主意,请改用setTimeout,或者在不需要延迟的情况下直接运行该函数.

Also, as gdoron says, setting a interval of nothing isn't really valid, and not a really good idea either, use setTimeout instead, or just run the function outright if no delay is needed.

        come();
        clearInterval(timer);
        timer = setInterval(come, 10000);

这篇关于setInterval(function(),time)在运行时更改时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆