jquery - How to check if a variable is a Timeout in Javascript? -
i decided contain code object separate out areas applied. advice here appreciated:
appconfig.loadelement , appconfig.cerrorelement these html elements:
<div id="loading" style="display:none;">loading...</div> <div id="cerror" style="display:none;">connection error.</div> var loadingtimeoutinstance = null, cerrortimeoutinstance = null, requestobj = { reset: function() { $(appconfig.loadelement).hide(); $(appconfig.cerrorelement).hide(); cleartimeout(loadingtimeoutinstance); cleartimeout(cerrortimeoutinstance); }, initiate: function() { loadingtimeoutinstance = settimeout(requestobj.timeout, appconfig.loadingdelayms); }, timeout: function () { cleartimeout(loadingtimeoutinstance); $(appconfig.loadelement).show(); cerrortimeoutinstance = settimeout(requestobj.cerror, appconfig.cerrordelayms); }, cerror: function () { cleartimeout(cerrortimeoutinstance); $(appconfig.loadelement).hide(); $(appconfig.cerrorelement).show(); } }
an implementation so:
when asynchronous request made:
requestobj.reset(); requestobj.initiate();
after response returned:
requestobj.reset();
the problems have identified in requestobj.reset(): - why hide elements if hidden? - can't clear timeout vars (loadingtimeoutinstance , cerrortimeoutinstance) if not set timeouts yet - causes not work.
you don't need check if it's of type... is.
if(cerrortimeoutinstance) cleartimeout(cerrortimeoutinstance);
Comments
Post a Comment