Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  • Thread Tools
  • Rate This Thread
  1. #1
    New 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>

  2. #2
    New Coder
    Join Date
    Feb 2016
    Posts
    80
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Try this:
    Code:
    $(function() {
        window.setTimeout(cycleBackgrounds, 5000);
    });

  3. Users who have thanked Sempervivum for this post:

    bobx2 (03-04-2016)

  4. #3
    New 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.

  5. #4
    Master Coder felgall's Avatar
    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.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •