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

    Question How do I kill a script after a time interval?

    I have a script that runs when the page is loaded, but I'd like it stop running after a specific time interval.

    I think I need to use this:

    Code:
    var t=setTimeout("javascript statement",milliseconds);
    I understand the milliseconds, that's the time delay. What I don't get is what do I put inside the "javascript statement" to kill that specific script?

    Here's the script:

    Code:
    <script type="text/javascript">
    
    /***********************************************
    * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    function disableSelection(target){
    if (typeof target.onselectstart!="undefined") //IE route
    	target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    	target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
    	target.onmousedown=function(){return false}
    target.style.cursor = "default"
    }
    
    </script>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Code:
    window.setTimeout(function(){ window.disableSelection = function(target) {};}, milliseconds);

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Since you haven't shown it, I presume this function is called in something resembling this form:

    disableSelection( document.getElementById( 'myField' ) );

    So to cancel the functionality after 10 seconds you could do:

    Code:
    setTimeout( function ()
    {
      var elem = document.getElementById( 'myField' );
    
      elem.onselectstart = null;
      elem.onmousedoen = null;
      elem.style.MozUserSelect = "";
    
    }, 10000 );


  •  

    Posting Permissions

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