| | |
Javascript stop function problem
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hi!
of cause, you already know, i'm gonna say im new to javascript and blahblahblah.... anyway, its a simple changing images animation starting by clicking on button. However, i stuck on stopping animation by clicking "stop" button.... here is my code, please if somebody can take time to look at it, i would really appreciate it!
of cause, you already know, i'm gonna say im new to javascript and blahblahblah.... anyway, its a simple changing images animation starting by clicking on button. However, i stuck on stopping animation by clicking "stop" button.... here is my code, please if somebody can take time to look at it, i would really appreciate it!
html Syntax (Toggle Plain Text)
<script language="JavaScript"> // Preloaded images if (document.images) { demo1 = new Image(); demo1.src = "images/breakdance1.gif" demo2 = new Image(); demo2.src = "images/breakdance2.gif"; demo3 = new Image(); demo3.src = "images/breakdance3.gif"; demo4 = new Image(); demo4.src = "images/breakdance4.gif" demo5 = new Image(); demo5.src = "images/breakdance5.gif" demo6 = new Image(); demo6.src = "images/breakdance6.gif" demo7 = new Image(); demo7.src = "images/breakdance7.gif" demo8 = new Image(); demo8.src = "images/breakdance8.gif" } // Reusable timer function timeimgs(numb) { thetimer = setTimeout("imgturn('" +numb+ "')", 1000); } // Reusable image turner function imgturn(numb) { if (document.images) { // This will loop the image if (numb == "8") { document["demo"].src = eval("demo8.src"); timeimgs('1'); } else { document["demo"].src = eval("demo" + numb + ".src"); timeimgs(numb = ++numb); } } } </script> <body> <img src="images/breakdance1.gif" name="demo"/> <form action=""><p> <input type="button" value="Watch this lesson" onclick="timeimgs('2');"/><input type="button" value="stop" onclick="clearInterval(timeimgs);"/> </p></form> </body>
Last edited by peter_budo; Nov 5th, 2009 at 7:15 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
1
#2 Nov 5th, 2009
the way you are using clearInterval is not correct.
its should be something like this:
for more information in this, just see:
http://www.w3schools.com/js/js_timing.asp
its should be something like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var t; //to start t=setTimeout("someFunction()",1000); //to stop clearTimeout(t);
http://www.w3schools.com/js/js_timing.asp
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
•
•
•
•
the way you are using clearInterval is not correct.
its should be something like this:
for more information in this, just see:JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var t; //to start t=setTimeout("someFunction()",1000); //to stop clearTimeout(t);
http://www.w3schools.com/js/js_timing.asp
Thank YOU so much for quick reply! it worked... the only thing is, IT DOES work in Mozilla but not in IE....it driving me banans
0
#4 Nov 5th, 2009
Tatyana,
Javascript allows arrays to have properties and methods in addition to their normal elements [0], [1], [2] etc.
Hence we can define all the functions associated with the breakdance animation as methods of a breakdance array, which also holds all the preloaded images. Each function is fairly simple.
This approach exposes just one variable (breakdance) to the global namespace.
If you wanted two or more animations on the same page, then it would be a fairly small step to convert the code into a reusable prototype (class) such that you could avoid duplicating everything for each animation.
This should work in all modern browsers.
It will take a bit of time to get your mind round this but it will be worthwhile in the longrun. Your javascript will improve by learning fom other people's code.
Lastly, there are many other good javascript solutions to this problem. This is just one.
Airshow
Javascript allows arrays to have properties and methods in addition to their normal elements [0], [1], [2] etc.
Hence we can define all the functions associated with the breakdance animation as methods of a breakdance array, which also holds all the preloaded images. Each function is fairly simple.
This approach exposes just one variable (breakdance) to the global namespace.
If you wanted two or more animations on the same page, then it would be a fairly small step to convert the code into a reusable prototype (class) such that you could avoid duplicating everything for each animation.
javascript Syntax (Toggle Plain Text)
var breakdance = [];//New Array breakdance.allow = false;//This allows/disallows image rotation. breakdance.numb = 0;//Counter. breakdance.tim = null;//Opaque setTimeout variable. breakdance.img = null;//The <img> element in which the animation will show. breakdance.delay = 500;//Default animation delay (can be overridden with .setDelay() method). breakdance.imgturn = function(){//Function that performs the animation. if (document.images && breakdance.img){ breakdance.img.src = breakdance[breakdance.numb].src; breakdance.numb = ++breakdance.numb % breakdance.length;//Increment counter, with wrap-around back to 0. clearTimeout(breakdance.tim);//In case there's already another timeout in waiting. if(breakdance.allow) { breakdance.tim = setTimeout('breakdance.imgturn()', breakdance.delay); }//Call breakdance.imgturn() again after delay. } }; breakdance.start = function(){//Start the animation breakdance.allow = true;//Allow it. breakdance.imgturn();//Start it. }; breakdance.stop = function(){//Stop the animation clearTimeout(breakdance.tim);//Clear any timeout currently in waiting breakdance.allow = false; }; breakdance.rewind = function(){ breakdance.stop();//Stop the animation breakdance.numb = 0;//Reset counter to zero breakdance.imgturn();//Show the first image in the rotation. }; breakdance.setImg = function(id){ if(!id) { return; } breakdance.img = document.getElementById(id);//Set id of required <img> element if(!breakdance.img) { alert('img id=' + id + ' not found on the page'); } }; breakdance.setDelay = function(x){//Set animation delay in milliseconds breakdance.delay = Number(x); }; breakdance.preload = function(n, prefix, suffix){ if(!n){ return; } prefix = (!prefix) ? '' : prefix; suffix = (!suffix) ? '.gif' : suffix; if (document.images){ for(var i=0; i<n; i++){ breakdance[i] = new Image(); breakdance[i].src = prefix + (i+1) + suffix;//Preload images into breakdance array. } } }; onload = function(){//A function which executes when the page has loaded, initialising breakdance. //As far as possible we control the behaviour here rather than "hard code" it in the methods. breakdance.preload(8, 'images/breakdance', '.gif');//preload images breakdance.setDelay(250);//override hardcoded default. breakdance.setImg('demo'); breakdance.rewind(); }
html Syntax (Toggle Plain Text)
<img id="demo" src="images/breakdance1.gif" /> <form action=""> <p> <input type="button" value="Rewind" onclick="breakdance.rewind();"/> <input type="button" value="Start" onclick="breakdance.start();"/> <input type="button" value="Stop" onclick="breakdance.stop()"/> </p> </form>
It will take a bit of time to get your mind round this but it will be worthwhile in the longrun. Your javascript will improve by learning fom other people's code.
Lastly, there are many other good javascript solutions to this problem. This is just one.
Airshow
Last edited by Airshow; Nov 5th, 2009 at 6:58 pm.
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- Javascript call VB function (VB.NET)
- HTML Post problem (HTML and CSS)
- javascript confirm() function problem???? (JavaScript / DHTML / AJAX)
- JavaScript + Google Adsense Problem (JavaScript / DHTML / AJAX)
- Javascript to stop Background Audio in html (JavaScript / DHTML / AJAX)
- Javascript - how to stop/halt/break this loop/script plss (JavaScript / DHTML / AJAX)
- Ask Function problem?thanks (C)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: HELP PLZ. content appear/disappear
- Next Thread: creat website
Views: 422 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxexample ajaxjspservlets array autoplay blackjack browser captchaformproblem checkbox child class close codes date debugger dependent developer disablefirebug dom editor element embed engine events explorer ext file flash form forms game gears getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe internet java javascript javascripthelp2020 jquery jsf jsfile jump libcurl maps margin marquee masterpage math matrixcaptcha media mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player position post programming rated redirect runtime safari scriptlets scroll search security session shopping size software solutions sources star stars stretch synchronous toggle tweet unicode variables web webkit webservice window wysiwyg \n





