force setInterval to load on page load


I am using a setInterval function to load some ajax which refreshes a div. All of this works except I need the setInterval to load right away and not first what for its set time. I have tried many things but nothing works. It seems once the time is set it cannot be changed...

setInterval(function refreshSlogan() {
	  type: "GET",
	  url: "../../mod/my1_theme/views/default/my_pages/slogans.php",
	  dataType: "script",
	  success: function(Response) {

}, 5000);

Open in new window

Who is Participating?
HainKurtConnect With a Mentor Sr. System AnalystCommented:
one way is this:

<body onload="refreshSlogan()" ...>
HainKurtSr. System AnalystCommented:
other way is :

   // ... other functions here if you have this code on your page
Gurvinder Pal SinghCommented:
Did you tried resetting the time interval?
This link has an example
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

waffeAuthor Commented:
Ok, I had to make a duplicate of the whole function and move it out of the setInterval function for <body onload="refreshSlogan()" ...> to work.

If I use this as a wrapper around the setInterval it does not work
setInterval(function refreshSlogan() {} }

I looked on the page hard to find what your talking about - I have seen a syntax like:
setInterval(function, [time1, time2]) but it does not work.

Thanks, I'll go with the first one even though I have to copy the same code twice.
Gurvinder Pal SinghCommented:
I was talking about this syntax

function() {
    playSlideshow =  setInterval( "slideSwitch()", 5000 );

this should reset the interval

Anyway, you have got the solution.

HainKurtSr. System AnalystCommented:
just add this



use this

<body onload="refreshSlogan()" ...>

I could not get the issues related to these two options...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.