login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > JS > Overige > afterLoad

afterLoad

Auteur: GJ2086 - 28 augustus 2011 - 03:42 - Gekeurd door: Koen - Hits: 5235 - Aantal punten: 3.00 (1 stem)





Voer methode2 pas uit nadat methode1 klaar is met laden.

Voorbeeld en documentatie van gebruik:

  1. // Het object
  2. testObject = function(){
  3. var data = '';
  4. // Methode met een vertraging
  5. this.testMethod1 = function(){
  6. // Geef aan dat testMethod1 bezig is met uitvoeren
  7. process['testMethod1'] = true;
  8. setTimeout(
  9. (function(){
  10. // Geef aan dat testMethod1 klaar is met uitvoeren
  11. process['testMethod1'] = false;
  12. data = 'This is some dummy content';
  13. }), 3000
  14. );
  15. }
  16. // Schrijf de waarde van data op het scherm
  17. this.testMethod2 = function(){
  18. document.write(data); // This is some dummy content
  19. }
  20. }
  21.  
  22.  
  23. // Creeer een instantie van het object
  24. object = new testObject();
  25. // Voer de methode met de vertraging uit
  26. object.testMethod1();
  27. // Voer testMethod2 uit wanneer testMethod1 klaar is met uitvoeren
  28. // Wanneer er geen gebruik wordt gemaakt van de afterLoad methode wordt er een lege string op het scherm geprint.
  29. object.afterProcess(
  30. 'testMethod1',
  31. (function(){
  32. object.testMethod2();
  33. })
  34. );

Code:
  1. /**
  2.  * Extend all objects with the process property
  3.  *
  4.  * @author Gert-Jan Jansma <gj.jansma@gmail.com>
  5.  * @param Object Container for process names
  6.  */
  7. Object.prototype.process = {};
  8.  
  9. /**
  10.  * Extend all objects with the afterProcess Method
  11.  *
  12.  * @author Gert-Jan Jansma <gj.jansma@gmail.com>
  13.  * @param string processName Name of the process
  14.  * @param function fn Function to be executed after a process
  15.  * @param int interval Delay time in microseconds
  16.  */
  17. Object.prototype.afterProcess = function(processName, fn, interval){
  18. var afterProcess = setInterval(
  19. (function(){
  20. if(process[processName] === false){
  21. fn();
  22. clearInterval(afterProcess);
  23. }
  24. }), ((interval) ? interval : 100)
  25. );
  26. };

Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (1)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.016s