Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
Results 1 to 4 of 4
-
03-03-2016, 08:12 PM #1New to the CF scene
- Join Date
- Mar 2016
- Posts
- 5
- Thanks
- 1
- Thanked 0 Times in 0 Posts
Need a time delay for a js function
I hate to ask this, I like figuring things out myself, it's the best way to learn for me but I've visited many sites & posts on here about delaying a js script but can't get any codes to work. I have a an image cycler I want to delay start up on. This is the script I have, could I plz get the correct way to code this to do that? Thx for any help!
<script>
function cycleBackgrounds() {
var index = 0;
$slideShows = $('.slideshow');
$imageEls = $('.toggle-image');
setInterval(function() {
index = index + 1 < ($imageEls.length / 2) ? index + 1 : 0;
$slideShows.each(function() {
$(this).children('.toggle-image').eq(index).addClass('show');
$(this).children('.toggle-image').eq(index - 1).removeClass('show');
});
}, 33000);
};
$(function() {
cycleBackgrounds();
});
</script>
-
03-03-2016, 09:33 PM #2New Coder
- Join Date
- Feb 2016
- Posts
- 80
- Thanks
- 0
- Thanked 16 Times in 16 Posts
Try this:
Code:$(function() { window.setTimeout(cycleBackgrounds, 5000); });
-
Users who have thanked Sempervivum for this post:
bobx2 (03-04-2016)
-
03-04-2016, 07:35 AM #3New to the CF scene
- Join Date
- Mar 2016
- Posts
- 5
- Thanks
- 1
- Thanked 0 Times in 0 Posts
Thx, Semper, that was what I needed, I was plugging codes in all the wrong locations & js is a new territory for me, not as easy as css or html to understand.
-
03-04-2016, 08:09 AM #4Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
setTimeout - runs once after the specified time (unless clearTimeout on it is run first).
setInterval runs continuously with the specified delay between executions until clearInterval is run.
In both cases there may be a slightly longer delay if JavaScript is busy running something else at the time (but setInterval starts its count over at the designated time even if the code runs slightly late - so it can catch up the next time.Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.



Reply With Quote
