jquery - Clear javascript on dynamic load -


i have javascript plugin special image scroller. scroller contains bunch of timeout methods , lot of variables values set timeouts.

everything works perfectly, site working on required pages loaded dynamically. problem when instance change language on site made jquery load function meaning content dynamically loaded onto site - , image slider aswell.

now here big problem! when load image slider second time dynamically previous values remains timers , else. is there way clear in javascript plugin if page reload?

i have tried lot of stuff far little appreciated!

thanks lot!

you might want reload scripts:

<script class="persistent" type="text/javascript">         function reloadscripts()   { [].foreach.call(document.queryselectorall('script:not(.persistent)'), function(oldscript)     {        var newscript = document.createelement('script');        newscript.text = oldscript.text;       for(var i=0; i<oldscript.attributes.length; i++)         newscript.setattribute(oldscript.attributes[i].name, oldscript.attributes[i].value);        oldscript.parentelement.replacechild(newscript, oldscript);     });   }    // test   setinterval(reloadscripts, 5000); </script> 

as far know, there's no other way reset script remove old 1 , create 1 same attributes , content. not clone node reset script, @ least in firefox.

you said want reset timers. mean cleartimeout() , clearinterval()? methods window.prototype.settimeout() , window.prototype.setinterval() both return id wich pass subsequent call of cleartimeout(). unfortunately there no builtin clear active timer.

i've wrote code register timer ids. simple todo-task implement wrapper callback settimeout open yet. functionality isn't faulty, excessive calls settimeout mess array.

be aware extending prototypes of host objects can cause undefined behavior since exposing host prototypes , internal behavior not part of specification of w3c. browsers change future. alternative put code directly window object, however, it's not absolutely sure other scripts call modified methods. both decisions not optimal choice.

  (function()   { // missing in older browsers, e.g. ie<9     if(!array.prototype.indexof)       object.defineproperty(array.prototype, 'indexof', {value: function(needle, fromindex)       { // todo: assert fromindex undefined or integer >-1         for(var i=fromindex || 0; < this.length && id !== window.settimeout.allids[i];) i++;         return < this.length ? : -1;       }});      if(!array.prototype.remove)       object.defineproperty(array.prototype, 'remove', { value: function(needle)        { var = this.indexof(needle);         return -1 === ? void(0) : this.splice(i, 1)[0];        }});       // warning: extensions prototypes of host objects window can cause errors     //          since expose , behavior of host prototypes not obligatory in     //          w3c specs.     //          can extend specific window/frame itself, however, other scripts     //          around when call window.prototype's methods directly.     try     {       var         oldst = settimeout,         oldsi = setinterval,         oldct = cleartimeout,         oldci = clearinterval       ;       object.defineproperties(window.prototype,        {         // todo: write wrapper removes id list when callback executed         'settimeout':         { value: function(callback, delay)           {             return window.settimeout.allids[window.settimeout.allids.length]               = window.settimeout.oldfunction.call(this, callback, delay);           }         },          'setinterval':         { value: function(callback, interval)           {             return window.setinterval.allids[this.setinterval.allids.length]               = window.setinterval.oldfunction.call(this, callback, interval);           }         },          'cleartimeout':         { value: function(id)           { debugger;             window.cleartimeout.oldfunction.call(this, id);             window.settimeout.allids.remove(id);           }         },          'clearinterval':         { value: function(id)           {             window.clearinterval.oldfunction.call(this, id);             window.setinterval.allids.remove(id);           }         },          'cleartimeoutall' : { value: function() { while(this.settimeout .allids.length) this.cleartimeout (this.settimeout .allids[0]); } },         'clearintervalall': { value: function() { while(this.setinterval.allids.length) this.clearinterval(this.setinterval.allids[0]); } },         'clearalltimers'  : { value: function() { this.clearintervalall(); this.cleartimeoutall(); } }       });        window.settimeout   .allids      = [];       window.setinterval  .allids      = [];       window.settimeout   .oldfunction = oldst;       window.setinterval  .oldfunction = oldsi;       window.cleartimeout .oldfunction = oldct;       window.clearinterval.oldfunction = oldci;     }      catch(e){ console.log('something went wrong while extending host object window.prototype.\n', e); }   })(); 

this puts wrapper method around each of native methods. call native functions , track returned ids in array in function objects of methods. remember implement todos.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -