Saturday, 15 September 2012

javascript - How to do callback correctly in specific conditions? -



javascript - How to do callback correctly in specific conditions? -

i have next goal:

i want have 2 functions - start() , end(). first function runned @ start of myfunc(), sec runned @ end of myfunc(). , end() shouldn't run while start() isn't completed.

now wrote next code:

function myfunc(onmyfuncready) { start(); // onmyfuncready(); } function start() { // start specific actions } function end() { // end specific actions } myfunc(function() { end(); });

but start() running asynchronously in relation end(). how can solve problem?

update1: check order of functions running , find ok, in start() utilize jquery animate function, run asynchronously...

update2: have next problem: want run end() when start() (where jquery animate() used) , myfunc() completed. both. hard task me.

the ways see them two:

first wait until not receive response start() , proceed. 1 time finished, pass end function parameter. unfortunately, should separate somehow bahviour of start/end. reason behind need verify if start has finished. kind of problem should not have end (release) function.

the sec way create variable value. 1 time assigned start, know going on. however, if start fails somehow you'll should not want proceed. 1 time start done, assign null value variable.after should check null , pass @ end release function.

a illustration of jerry cain's stanford course, here: https://www.youtube.com/watch?v=yr1ynovg-4g&feature=playlist&p=9d558d49ca734a02&index=6

i hope helped you. cheers , think synchronous way if have dependencies , utilize resources.

javascript jquery callback

No comments:

Post a Comment