Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 31st, 2008, 04:56 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sakthi Send a message via Yahoo to sakthi
Default stop current javascript function using javascript

i have two function here;

function changeImage(){
...
...
setTimeout('changeImage()',1000);
}
function stopChangeImage(){
  .../*
   here i want script to stop the above function. that function was called when page loading
 ....*/

}

if anyone know. please reply me. i hope most of the people known this script.


 
Old May 31st, 2008, 05:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Not 100% sure what you want but setTimeout returns an integer, you can save this id and call clearTimeout(id) to prevent the function from starting. This doesn't stop it in mid call though, just stops it kicking off.

--

Joe (Microsoft MVP - XML)
 
Old June 2nd, 2008, 03:07 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Joe is right with clearTimeout. The other way is to use some simple logic within changeImage to decide whether to actually change it or not. Something like

var doChange = true;

function stopChangeImage() {
  doChange = false;
}

function changeImage() {
  if(doChange) {
    // ... code to perform change ...
  }
}

 
Old June 2nd, 2008, 03:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yes, Joe is right, but it's easy to mess this up.

You *MUST* make the variable that holds the "handle" to the timeout *global* to the page. Too often, people just make it local to the function that holds the window.open() call and then it doesn't work.

Also, if a timeout is already active, setting a *new* timeout does *NOT* clear the prior one! So...

<script>
var changeImageTimeout = null;

function changeImage()
{
    ...
    // you might not need this, but it's not a bad idea:
    if ( changeImageTimeout != null ) clearTimeout(changeImageTimeout);
    changeImageTimeout = setTimeout('changeImage()',1000);
}
function stopChangeImage()
{
    // don't call clear if it's not set...
    if ( changeImageTimeout != null ) clearTimeout(changeImageTimeout);
    changeImageTimeout = null; // tell yourself nothing is active
}
</script>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript return false won't stop post rstelma ASP.NET 1.0 and 1.1 Basics 9 March 16th, 2006 02:25 PM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
get value from javascript function suzila ASP.NET 1.0 and 1.1 Basics 6 December 3rd, 2005 10:38 AM
javascript "stop" syntax - needs declared? crmpicco Javascript 5 September 14th, 2005 06:01 AM
Problem with a Javascript function SuMajestad PHP How-To 4 August 17th, 2004 09:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.