Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 31st, 2008, 05:56 AM
Registered User
 
Join Date: Sep 2006
Location: noida, uttar pradesh, India.
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.


Reply With Quote
  #2 (permalink)  
Old May 31st, 2008, 06:55 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old June 2nd, 2008, 04:07 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
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 ...
  }
}

Reply With Quote
  #4 (permalink)  
Old June 2nd, 2008, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
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>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 07:01 AM
Problem with a Javascript function SuMajestad PHP How-To 4 August 17th, 2004 10:04 PM



All times are GMT -4. The time now is 04:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.