javascript - Calling a function asynchronously? -
i have expensive function, createnewwindow(). want show progress indicator when method gets called, , hide indicator when function finishes. i've tried though, don't see progress indicator until method has finshed, i'm trying call method asychronously (and failing).
i tried this:
$( "#submit" ).click( function () { $("#progress").show(); $.deferred(createnewwindow()); } );
and last line of createnewwindow $("#progress").show(). didn't work. before tried:
$( "#submit" ).click( function () { $("#progress").show(); settimeout(createnewwindow(), 0); } );
same effect.
what's right way this?
i saw few other posts this describe using setinterval or settimeout, unable them work expecting.
this common mistake. problem right here:
settimeout(createnewwindow(), 0);
this call createnewwindow
, pass result (probably void) settimeout
. that's not want, want pass function createnewwindow
settimeout
call @ later time:
settimeout(createnewwindow, 0);
note missing parentheses. you're passing value of createnewwindow
(which function) settimeout
, instead of calling , passing return value.
this should make progress bar show right away. however, if createnewwindow
expensive function eating lots of resources, will still slow down web page since javascript code run in same thread. in case, might want consider using worker
.
Comments
Post a Comment