473,407 Members | 2,359 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,407 software developers and data experts.

How can I stop a function so it can restart again?

Hi,

I have this function:

Expand|Select|Wrap|Line Numbers
  1. function startAnimation() {
  2.  
  3.         lastVertex = 0;
  4.         k=0;
  5.         i = 0;
  6.       step = 5; 
  7.       tick = 10; 
  8.       stepnum=0;
  9.       speed = "";
  10.         eol=poly.Distance();
  11.         map.setCenter(poly.getVertex(0),16);
  12.         map.addOverlay(new GMarker(poly.getVertex(0),G_START_ICON));
  13.         map.addOverlay(new GMarker(poly.getVertex(poly.getVertexCount()-1),G_END_ICON));
  14.         if (supportsCanvas) {
  15.           marker = new ELabel(poly.getVertex(0), '<canvas id="carcanvas" width="32" height="32"><\/canvas>',null,new GSize(-16,16));
  16.             map.addOverlay(marker);
  17.           canvas = document.getElementById("carcanvas").getContext('2d');
  18.           var p0 = poly.getVertex(0);
  19.             var p1 = poly.getVertex(1);
  20.           angle = bearing(p0,p1);
  21.           plotcar();
  22.         } else {
  23.           marker = new GMarker(poly.getVertex(0),{icon:car});
  24.           map.addOverlay(marker);
  25.         }
  26.         poly2 = new GPolyline([poly.getVertex(0), poly.getVertex(1), poly.getVertex(2)], 0,0);
  27. //        map.addOverlay(poly2);
  28.  
  29.         setTimeout("animate(0)",2000);  // Allow time for the initial map display
  30.  
  31. }
that is fired by this button:
Expand|Select|Wrap|Line Numbers
  1. <input type="button" onclick="startAnimation()" value="Show Bus Route 1" style="width:120px; height:24px; text-align:center">
but if you click the button again, the original function continues working while the second one executes.

If you want to see it in action, it's here

Is there some way that that button click can say "stop the first one and start the next one"?

thanks for your help.
Dec 10 '10 #1
9 3620
JKing
1,206 Expert 1GB
Try creating a reset function that will return all values to their starting position. Then call this reset function at the start of your startAnimation function.
Dec 10 '10 #2
thanks, JKing

but assuming that those variables would just be the global variables and without the var part, it would look like this:

Expand|Select|Wrap|Line Numbers
  1. function reSet() { 
  2. lastVertex = 0;
  3.         k=1;
  4.         i = 0;
  5.       step = 5; 
  6.       tick = 10; 
  7.       stepnum=1;
  8.       speed = "";
  9.       }
which doesn't seem to do anything. Or were you talking about something else?
Dec 10 '10 #3
JKing
1,206 Expert 1GB
Well when I tried the example you gave it appears as if the bus tries to run its route starting from where ever it left off last. What I was thinking is that you need to reposition the bus to the start and then run its route or alternatively and more complexly find where the bus is on its route if it has already been activated and continue on from the last point.
Dec 10 '10 #4
totally.

but how? There's not some sort of "clear everything" command?
Dec 10 '10 #5
JKing
1,206 Expert 1GB
clearOverlays()?
Dec 10 '10 #6
assuming you would call that like this:
Expand|Select|Wrap|Line Numbers
  1. onclick="startAnimation();clearOverlays()"
it doesn't appear to do anything

Here's what I figure (because I obviously didn't write the code myself):

If you let the bus run til the end of its course then click the button again, it runs smoothly - the map pans correctly and you can see the angle of the bus moving as if it were on the road, but it stays in the same spot.

So it looks like all the other functions are reinitiating, just not the one that actually moves the bus.

Now, if you look at it on IE7 (or some other browser that doesn't support canvases) you can see that if you let the bus get to the end and then click the button, the original bus will stay at the end but a second bus appears and it follows the route fine.

but if you click halfway though the route, the second bus appears and they both try to run the course which makes for weirdness.

So what all that tells me is that I'm looking for:
1)a way to cancel the function with the onclick, and
2)a way to reset the canvas position if canvases are supoorted.

which doesn't help me at all, but it may mean something to you...
Dec 10 '10 #7
JKing
1,206 Expert 1GB
Sorry I should have elaborated. Not sure what version of the google maps api you are using but with a quick scan through version 2 documentation I noticed there is a clearOverlays function.

Try adding it to the start of your function.
Expand|Select|Wrap|Line Numbers
  1. map.clearOverlays();
  2.  
Dec 10 '10 #8
closer... have a look here:
http://xelawho.com/map/moving3.htm

at least now we get two buses, weird and shaky as they are... but the second one is following the bearings of the first, which says to me that the original function is still operating...
Dec 10 '10 #9
what about return; ?

doesn't that do something like abort?
Dec 10 '10 #10

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

Similar topics

9
by: Sims | last post by:
Hi, I recall a function that would tell me how long it took to generate an output. But for the life of me I cannot remember it. Dow anybody know it? Or do I have to write something myself,...
13
by: python | last post by:
hello and thanks for reading this, i have been a dos/windows user using some form of the basic language for 30 years now. i own and run a small programming company and there is one feature that...
2
by: Jay | last post by:
Well this is my function to split the data into its own lines Sub preSift(ByVal input As String) Dim spl() As String spl = New String() {} spl = Split(input, ControlChars.NewLine) ', ,...
2
by: User | last post by:
Hi, What is the best way to release all resources holded by the Timer (myTimer from class System.Timers.Timer)? Is it: 1- myTimer.dispose 2- myTimer.enabled = false 3- myTimer.close
1
by: Primera | last post by:
I have an application that fixes some common problems that prevent the SMS Advanced Client from operating correctly. During this application I need to stop the Windows Management Instrumentation...
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...
9
by: Gurbinder Kaur | last post by:
Hi, i have made a project for printing fee receipt. Using Windows-XP, VBasic 6.0 & Ms-Access-97. i have designed a form for prinring receipts. m paper-size is 5x10. Two copies of bills in this...
1
by: aj | last post by:
A few service stop/start/restart questions on SQL Server 2005 SP2, which I'll call SQLS. It looks as if there are *potentially* 6 ways to start/stop SQLS Services like the engine itself,...
18
by: Aaron Gray | last post by:
I know this has probably been argued to death, but I am going to raise it fresh again, and basically lets have an unofficial 'isArray()' contest that we can hopefully put it to rest as best as we...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.