473,491 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

stopping setInterval()

21 New Member
i'm making a javascript visualization effect, and i'm having some trouble with the stopping the main function when a certain particle reaches 100% height:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4.   <meta content="text/html; charset=ISO-8859-1"
  5.  http-equiv="content-type">
  6.   <title>$P4rk5</title>
  7.   <script>
  8. function fetchById(id){
  9. if (document.getElementById){
  10. var fetch = document.getElementById(id);
  11. }
  12. else if (document.layers){
  13. var fetch = document.layers[id];
  14. }
  15. else if (document.all){
  16. var fetch = document.all[id];
  17. }
  18. return fetch;
  19. }
  20. function main(id){
  21. position = new Array();
  22. position[0] = parseFloat(fetchById(id).style.left);
  23. position[1] = parseFloat(fetchById(id).style.bottom);
  24. position[1] = position[1] + 4;
  25. fetchById(id).style.bottom = position[1] + "%";
  26. fetchById(id).style.opacity = parseFloat(fetchById(id).style.opacity) - .04;
  27. if(parseFloat(fetchById(id).style.left) < 50){
  28. fetchById(id).style.left = parseFloat(fetchById(id).style.left) + 2 + "%";
  29. }
  30. else{
  31. fetchById(id).style.left = parseFloat(fetchById(id).style.left) - 2 + "%";
  32. }
  33. if(position[1] < 100){
  34. setInterval("main('p" + x + "')", 0);
  35. }
  36. }
  37.   </script>
  38. </head>
  39. <body
  40.  style="color: rgb(51, 204, 255); background-color: rgb(0, 0, 0); height: 100%; overflow: hidden;"
  41.  alink="#3366ff" link="#3366ff" vlink="#3366ff">
  42. <div id="container">
  43. </div>
  44. <script>
  45. x = 0;
  46. y = 10;
  47. function create(){
  48. while(x < y){
  49. style = "z-index: " + Math.floor(Math.random()*100) + "; position: absolute; bottom: -10px; left: " + Math.floor(Math.random()*100) + "%; opacity: 1;";
  50. image = document.createElement('img');
  51. image.setAttribute('style', style);
  52. image.setAttribute('alt', 'p');
  53. image.setAttribute('src', 'particle.png');
  54. image.setAttribute('id', 'p' + x);
  55. fetchById('container').appendChild(image);
  56. setInterval("main('p" + x + "')", 1);
  57. x = x + 1;
  58. }
  59. y = y + 10;
  60. setTimeout("create()",500)
  61. }
  62. create()
  63. </script>
  64. </body>
  65. </html>
  66.  
here is a demo: nowpct.awardspace.com/sparks.php

thanks!
Jun 25 '07 #1
6 2098
naurus
21 New Member
sorry, i forgot to specify that i need to stop the setInterval("main('p" + x + "')", 1); function once that specific particle reaches 100% height

also, disregard the:
Expand|Select|Wrap|Line Numbers
  1. if(position[1] < 100){
  2. setInterval("main('p" + x + "')", 0);
  3. }
  4.  
in the main function, it's been taken out.
Jun 25 '07 #2
eboyjr14
27 New Member
When you call setInterval(), save the returned number. (let's say it is setVar )

Then you would call clearInterval( setVar ); to stop it.
Jun 26 '07 #3
naurus
21 New Member
is there a way to stop a setInterval after you've called it?
Jun 27 '07 #4
naurus
21 New Member
thanks a ton!!!!! i never would've found that! i'll update the demo soon to show the finished product
Jun 27 '07 #5
gits
5,390 Recognized Expert Moderator Expert
hi ...

of course ;) have a look:

Expand|Select|Wrap|Line Numbers
  1. var interval = window.setInterval(func_ref, milliseconds);
  2.  
  3. // stop the interval - the above variable holds the reference to it
  4. window.clearInterval(interval);
kind regards ...
Jun 27 '07 #6
acoder
16,027 Recognized Expert Moderator MVP
Merged threads.
Jun 27 '07 #7

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

Similar topics

10
7736
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval...
1
16079
by: Weston C | last post by:
In the course of trying to build a simple clock, I've run into a problem using the setInterval (and setTimeout) function. http://weston.canncentral.org/misc/tkeep/tkeep.html...
17
2089
by: George Hester | last post by:
Hoe can I use setInterval where its argument is a function which also has an argument? For example I have a function change(Msg) where Msg is dynamically generated and returns nothing at this point...
6
15184
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
4
3234
by: barry | last post by:
If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load...
4
1621
by: the other john | last post by:
I have a function that is activated by a mouseover. The function triggers an image rotation. I need to stop the rotation on the mouseout but I don't know how to do this. the mouseover triggers...
3
5768
by: shawn | last post by:
Hi All Was trying to get this bit of code working (simplified of course) for (i=0;i<uuid_num;i++) { window.setInterval(InitMap(uuidValues), timePeriod); } Where uuid_num, uuidValues,...
1
2470
by: jonahmason | last post by:
hi. a javascript newbie here in dire need of help. for the past 3 days i've been trying to get a script using setinterval to work but a part of the code, that executes after setinterval hits a...
2
2757
by: shawnwperkins | last post by:
Hi Folks, I'm new to Javascript and just need a little help. I downloaded a script from Dynamic Drive's Web site and I'm trying to make a simple modification and could use some help. :) The...
0
7118
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
7192
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...
1
6862
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
7364
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
5452
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,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.