13

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);
});

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.

1
  • What is the problem? Your jsFiddle alerts initially and every 30 seconds thereafter exactly as the code specifies. setInterval does not call it's function until the time elapses. If you want it executed initially, then you just call it as you did. If it's a more complicated piece of logic you want called in both places, then just put it in a named function that you can call both places. Commented Sep 25, 2011 at 15:48

5 Answers 5

18

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

$(function() {
    sayHi();

    function sayHi() {
       setTimeout(sayHi,30000);
       alert('hi');
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Why not just use setInterval?
@user2019515 - I understood the OP wanted a single invocation of the function. $(function() { sayHi(); setInterval(sayHi,30000); function sayHi() { alert('hi');}); would also work but has separate instances of the function.
Question is related to setinterval function they why are you suggesting about settimeout function.
12

Wrap your code in a function. Then, pass the function as a first argument to setInterval, and call the function:

$(document).ready( function() {
    //Definition of the function (non-global, because of the previous line)
    function hi(){
        alert("hi");
    }

    //set an interval
    setInterval(hi, 30000);

    //Call the function
    hi();
});

Comments

4

Well, there probably is a way of doing it in just one call..

setInterval(
    (function x() {
        alert('hi');
        return x;
    })(), 30000);

Another solution would be to use arguments.callee, but as it is now deprecated, it is generally not recommended to use it.

setInterval(
    (function() {
        alert('hi');
        return arguments.callee;
    })(), 30000);

Comments

1

No, that's not possible with setInterval. You could use setTimeout for example:

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

$(function() {
    foo();
});

Comments

1
$(function() {

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

    heythere();

});

If you want it to only run once after 30 seconds, you are looking to use setTimeout not setInterval.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.