jquery - Other way to "wait" during javascript animation -



jquery - Other way to "wait" during javascript animation -

i'm looking way execute code :

$.each($("#gallery > img"), function(index,curimg) { settimeout(function() { clearcanvas(); cvsctx.drawimage(curimg,0,0); } , index*animationms); });

this code draw image gallery canvas every animationms . create possible stop animation, "play/stop" button, can't way... thought or workaround ?? give thanks !!

i find creating timeouts in loop hard manage - don't want have cancel multiple timeouts. improve have function doing work phone call (indirectly) setting timeout before completes, because can set in simple if test decide whether set next timeout , go on animation.

perhaps little this:

<input id="playpause" type="button" value="play"> <script> function initanimation(animationms, autorepeat, waitforplaybutton) { var currentframe = 0, $imglist = $("#gallery > img"), paused = waitforplaybutton; function drawnext() { clearcanvas(); cvsctx.drawimage($imglist[currentframe++],0,0); if (currentframe >= $imglist.length) { currentframe = 0; if (!autorepeat) { paused = true; $("playpause").prop("value", "play"); } } if (!paused) settimeout(drawnext, animationms); } $("playpause").prop("value", waitforplaybutton ? "play" : "pause") .click(function() { this.value = (paused = !paused) ? "play" : "pause"; if (!paused) drawnext(); }); if (!waitforplaybutton) drawnext(); } initanimation(100, true, false); </script>

if autorepeat param false animation run 1 time , stop, can restarted via button, otherwise (obviously) keeps repeating.

if waitforplaybutton false animation start immediately, otherwise (obviously) start when button pressed.

pressing button pause @ current frame.

(not tested since don't have bunch of images handy, i'm sure thought , can prepare problems yourself. or allow me know if errors...)

javascript jquery animation canvas settimeout

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -