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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set time limit for a function

    I use a js function that moves a background image and i want to stop this moving after a time.
    I tried to use setTimeout() and clearTimeout() methods, but it doesn't stop the movement.
    What is the problem?

    Code:
    <script type="text/javascript" language="javascript">
     
     
    function MoveBg()
    {
    some code
    }
     
    $(function() {
     
    var i = setTimeout("MoveBg()" ,200);
    setTimeout(function(){clearTimeout(i)},10000);
     
    });
     
            </script>

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    you could use jQuery.
    it's simpler this way. and you can call stop() at any time

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    Your function use probably a setTimeout. Then this setTimeout is to clear;
    Try this scheme :
    Code:
    // A global variable
    var myMove;
    // The function to repeat
    function MoveBg(){
      // some code
       myMove=setTimeout(MoveBg,200);
    }
    // to start the move 
    MoveBg();
    // to stop the move after 10 seconds
    setTimeout(function(){clearTimeout(myMove)},10000);
    It's probably better than 125ko for jQuery !
    Last edited by 007julien; 04-01-2013 at 02:50 PM.

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    Quote Originally Posted by 007julien
    It's probably better than 125ko for jQuery !
    91k and it's not 80's anymore. people use broadband, not dial-up, and average pic on the web is bigger than that

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    Ok for the 90,4 Ko. It's ever more that a change which almost no code consome...

  • #6
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    Quote Originally Posted by 007julien View Post
    Ok for the 90,4 Ko. It's ever more that a change which almost no code consome...
    man don't get so defensive. that was just a suggestion for the OP

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • #7
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    I am not offensive but just scared to see the use made of JQuery !


  •  

    Posting Permissions

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