JavaScript - jQuery间隔 [英] JavaScript - jQuery interval

查看:121
本文介绍了JavaScript - jQuery间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中使用JavaScript。我有以下脚本每隔30秒警告 hi

I am using JavaScript with jQuery. I have the following script to alert hi every 30 seconds.

$(document).ready( function() {
    alert("hi");
setInterval(function() {
    alert("hi");
}, 30000);
});

我想提醒 hi 页面时加载(文档/页面完全加载时)和之后每隔30秒(如 hi (0s) - hi (30s) - hi (60s)..等)。但我的解决方案适用于两个实例。一个在DOM准备好,另一个在循环中。有没有办法在单个实例中做同样的事情?

I want to alert hi when the page loads (when document / page gets completely loaded) and on every 30 seconds interval afterwards (like hi(0s) - hi(30s) - hi(60s).. etc). But my solution works on two instances. One on DOM ready and the other in a loop. Is there any way to do the same in a single instance?

你可以看到我的小提琴 这里

You can see my fiddle here.

推荐答案

您可以使用setTimeout代替并让回调重新安排。

You could use setTimeout instead and have the callback reschedule itself.

$(function() {
    sayHi();

    function sayHi() {
       setTimeout(sayHi,30000);
       alert('hi');
    }
});

这篇关于JavaScript - jQuery间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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