Partage
  • Partager sur Facebook
  • Partager sur Twitter

Fonction anonyme

Problème

Sujet résolu
    10 janvier 2011 à 23:00:18

    Bonsoir,

    Je voudrais encapsuler mon code javascript dans une fonction anonyme, mais impossible, mon script plante dès que j'encapsule le code.
    Quelque-chose dois m'échapper... mais quoi ?

    La console de Chrome me renvois ceci :

    Uncaught ReferenceError: snowscriptAnime is not defined
    


    Non encapsulé tout vas bien ( démo ici => http://www.phpixel.fr/jquerytest/neige/ ) mais voici la version qui ne fonctionne pas :



    <script type="text/javascript">
    (function() { 
    var bodyHeight = window.innerHeight;
    var bodyWidth = window.innerWidth;
    // Interval fonction snowscriptAnime()
    var interval = 40;
    var maxInterval = 60;
    var minInterval = 10;
    // Limites x et y
    var maxX = bodyWidth-20;
    var minX = 20;
    var maxY = bodyHeight-20;
    // Calcul du nombre de flocons
    var nbFlocon = Math.round(bodyWidth/100)*10;
    // On place les flocons dans le DOM
    for(i=0;i<=nbFlocon;i++){
    	document.write("<span id='flocon"+i+"' class='flocon'></span>");
    	// On initialise le flocon
    	snowscriptInitFlocon(i);
    }
    // On initialise la direction du vent ( de gauche à droite, 0 = neutre )
    var ventDirection = 0;
    // Annimation
    snowscriptAnime();
    
    /*
    ** Fonction chargée d'initialiser les flocons
    */
    function snowscriptInitFlocon(id){
    	var flocon = document.getElementById('flocon'+id);
    	// Position du flocon
    	flocon.posx = Math.floor(Math.random()*(maxX))+20;
    	flocon.posy = Math.floor(Math.random()*-400);
    	// Vitesse du flocon (multiplicateur de déplacement)
    	flocon.vitesse = Math.floor(Math.random()*3)+1;
    	// Opacité du flocon
    	flocon.opacite = Math.floor(Math.random()*10)+5;
    	// Taille du flocon
    	flocon.width = Math.floor(Math.random()*7)+3;
    	// Modification de la position
    	flocon.style.left = flocon.posx+"px";
    	flocon.style.top = flocon.posy+"px";
    	// Modification de l'opacité
    	flocon.style.opacity = flocon.opacite/10;
    	flocon.style.filter = 'alpha(opacity=' + flocon.opacite*10 + ')';
    	// Modification de la taille
    	flocon.style.backgroundSize = flocon.width+"px";
    }
    
    /*
    ** Fonction chargée de l'annimation des flocons
    */
    function snowscriptAnime(){
    	// Variation interval
    	var intervalTendance = Math.floor(Math.random()*3)+1;
    	if(intervalTendance == 1) interval = interval+1;
    	else if(intervalTendance == 2) interval = interval-1;
    	if(interval < minInterval) interval = minInterval;
    	else if(interval > maxInterval) interval = maxInterval;
    	// Variable décidant si le vent doit changer ( si l'annimation est trop rapide le vent ne change pas de direction )
    	var ventChange = (interval > 40) ? true : false;
    	// Boucle...
    	for(i=0;i<=25;i++){
    		var flocon = document.getElementById('flocon'+i);
    		// Si le flocon sort du DOM on le réinitialise
    		if(flocon.posy >= maxY || flocon.posx >= maxX || flocon.posx <= minX) snowscriptInitFlocon(i);
    		else{
    			// Tendance du vent
    			var ventTendance = Math.floor(Math.random()*3)+1;
    			// Modification de la direction du vent suivant la tendance et l'interval de la chutte
    			if(ventChange && ventTendance == 1) ventDirection = ventDirection+1;
    			else if(ventChange && ventTendance == 2) ventDirection = ventDirection-1;
    			else ventDirection = ventDirection;
    			// On détermine la nouvelle position y suivant la vitesse du flocon
    			flocon.posy = flocon.posy+flocon.vitesse;
    			// On détermine la nouvelle position x suivant la direction du vent...
    			// ... en tenant compte d'une zone neutre pour éviter les changements de direction trop fréquent
    			if(ventDirection < -10) flocon.posx = flocon.posx-1;
    			else if(ventDirection > 10) flocon.posx = flocon.posx+1;
    			// Modification de la position
    			flocon.style.top = flocon.posy+"px";
    			flocon.style.left = flocon.posx+"px";
    		}
    	}
    	// En boucle...
    	var timer = setTimeout("snowscriptAnime()", interval);
    }
    })();
    </script>
    
    • Partager sur Facebook
    • Partager sur Twitter
      10 janvier 2011 à 23:24:44

      Corrige ton setTimeout comme indiqué dans le tuto ci-dessous, et tout devrait rouler.

      Bonnes pratiques JavaScript
      • Partager sur Facebook
      • Partager sur Twitter

      Fonction anonyme

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown