473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How To Stop a Function

Expand|Select|Wrap|Line Numbers
  1. window.onload = initLinks;
  2.  
  3. function initLinks() {
  4.     document.getElementById("rotateBack").onclick = rotateBack;
  5.     document.getElementById("rotate").onclick = rotateFor;
  6. }
  7.  
  8. var thisAd = 0;
  9.  
  10. var adImages = new Array("catImages/1.png","catImages/2.png","catImages/3.png","shoeImages/4.png","catImages/5.png","catImages/6.png","catImages/7.png","catImages/8.png","catImages/9.png","catImages/10.png");
  11.  
  12. function rotateFor() {
  13.     thisAd++;
  14.     if (thisAd == adImages.length) {
  15.         thisAd = 0;
  16.     }
  17.     document.getElementById("adBanner").src = adImages[thisAd];
  18.     setTimeout(rotateFor, 500);
  19. }
  20.  
  21. function rotateBack() {
  22.     if (thisAd == 0) {
  23.         thisAd = adImages.length;
  24.     }
  25.     thisAd--;
  26.     document.getElementById("adBanner").src = adImages[thisAd];
  27.     setTimeout(rotateBack, 500);
  28. }
  29.  
Hi,

I'm a beginner and working through a JavaScript tutorial guide, while trying to develop a small project at the same time. A part I'm stuck on involves an animation that runs through a group of images and is triggered by a "rotateFor" button and another button that reverses the animation "rotateBack".

I've got each button to work individually in there respective directions, however when I press the second of either two buttons it obviously still remembers the previous function and just jumps back and forth.

I can't figure out how to cancel the previous function, I realise that it involves implementing something like rotateBack = null; into "rotateFor" function (for example) but I don't understand enough to implement this correctly.

Thanks in advance for any help,

Phil
Jan 22 '12 #1
1 2166
Rabbit
12,516 Expert Mod 8TB
Use the clearTimeout function to cancel it.
Jan 23 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: engsol | last post by:
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss loves the quick turn-around compared to C). Each log file may contain one...
12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
8
by: Eric Osman | last post by:
My javascript program has reason to want to stop. For example, function A has 5 lines, the second of which calls function B, which has 5 lines, the first of which calls function C. But...
2
by: Adrian MacNair | last post by:
Hi I need some help if anyone can understand my crap javascript. The problem is that after the slideshow ends (reaches the end of array) it should stop, but the timeout doesn't clear and I can see...
2
by: Maxwell_Smart | last post by:
Is there a way for a function to refer to itself generically? I'd like to use such a thing (should it exist) for convenience and consistency, not functionality. For example: Function...
2
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to start, pause and resume a recurrsive search exactly where you left off, say in the registry programmatically? -- Michael Bragg,...
2
by: shapper | last post by:
Hello, On the start of a function I need to verify a condition. If it is false I want the rest of the code to not run. My question is: should I put all the function code inside an IF or...
4
by: ravindarjobs | last post by:
hi...... i am using ms access 2003,vb6 i have a form. in that i have 2 buttons 1. start search 2 stop search when i click the "start search" button the fucntion SearchSystem() is called,...
3
by: vunet | last post by:
It has been widely used nowadays to wrap JavaScript libraries, as seen in JQuery. (function() { var global = this; // ... })(); The advantage, as I learned, is the isolation from other...
1
by: ziycon | last post by:
Is there a function to tell jquery to only do something on the first instance that matches then to stop. For example I add a class to a LI to an UL with the below but it adds the LI to all ULs...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.