force setInterval to load on page load

Hi,

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...


<script>
setInterval(function refreshSlogan() {
	$.ajax({
	  type: "GET",
	  url: "../../mod/my1_theme/views/default/my_pages/slogans.php",
	  dataType: "script",
	  success: function(Response) {
	  	//alert(Response);
		$("#slogan_rotate_txt").html(Response,"");
	  },
	});

}, 5000);
</script>

Open in new window

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

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

$(document).ready(function(){
   refreshSlogan();
   // ... other functions here if you have this code on your page
 });
0
 
Gurvinder Pal SinghCommented:
Did you tried resetting the time interval?
This link has an example
http://jonraasch.com/blog/a-simple-jquery-slideshow
0
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
$(document).ready(function(){
setInterval(function refreshSlogan() {} }

"gurvinder372:"
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.
0
 
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.

Thanks
0
 
HainKurtSr. System AnalystCommented:
just add this

$(document).ready(function(){
   refreshSlogan();
 });

or

use this

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

I could not get the issues related to these two options...
0
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.