[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

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

0
waffe
Asked:
waffe
  • 3
  • 2
1 Solution
 
HainKurtSr. 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now