View Full Version : How do I kill a script after a time interval?

07-16-2011, 06:22 AM
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:

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:

<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
else //All other route (ie: Opera)
target.onmousedown=function(){return false}
target.style.cursor = "default"


07-16-2011, 09:02 PM
window.setTimeout(function(){ window.disableSelection = function(target) {};}, milliseconds);

Logic Ali
07-17-2011, 04:37 AM
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:

setTimeout( function ()
var elem = document.getElementById( 'myField' );

elem.onselectstart = null;
elem.onmousedoen = null;
elem.style.MozUserSelect = "";

}, 10000 );