Probleme avec des setTimeout dans une class

astyan54 Messages postés 1 Date d'inscription vendredi 18 août 2006 Statut Membre Dernière intervention 17 février 2009 - 17 févr. 2009 à 14:08
PetoleTeam Messages postés 3426 Date d'inscription lundi 26 décembre 2005 Statut Membre Dernière intervention 14 janvier 2011 - 18 févr. 2009 à 17:03
je sais que mon sujet est loin d'etre le premier avec ce genre d'intitulé, mais j'ai beau chercher, je ne vois pas la solution a mon problème.

voila, je cherche a faire un compte a rebours. pour ce faire, j'ai fait une class cptrebour

le probleme, c'est que lorsque j'arrive dans le methode tour, this et indefini. j'ai bien vu des personne avec le meme probleme, mais leur solution ne marche pas (si j'ai bien appliquer leur conseilles)

voici mojn code :

var cp = new cptrebour(0,1,30,"alert('coucou')",ecrant);
cp.start();

cp.affbeforelt();

function cptrebour(heurs,min,sec,action,elt) {
    // Attributes
    this.h = str2int(heurs) ;
    this.m = str2int(min) ;
    this.s = str2int(sec) ;
    this.action = action;
    this.elt = elt;
    this.actif = true;
    // Methods
    this.decrsec = function()
    { // Returns sum of all ressources
        alert("lolilol");
        if (this.s > 0 )
        {
            this.s--;
        }
        else if (this.m > 0)
        {
            this.m--;
            this.s = 59;
        }
        else if(this.h > 0)
        {
            this.h--;
            this.m = 59;
            this.s = 59;
        }        else if (this.h 00 && this.m 00 && this.s == 00)
        {
            eval(this.action);
            this.h = -1;
            this.m = -1;
            this.s = -1;
            this.actif = false;
        }
    }
   
    this.tour = function()
    {
        var obj = this;
        alert(obj.actif);  <==  "undefined"
        if (obj.actif == true) <== ne passe jamais la boucle
        {
            obj.decrsec();
            window.setTimeout(obj.tour, 1000);
            obj.affbeforelt();
        }
    }
   
    this.start = function()
    {
        alert("start"); <== ok
        var obj = this;
        window.setTimeout(obj.tour, 1000);
        obj.actif = true;
    }
   
    this.aff = function()
    {
        elt.innerHTML =  this.tostring();
    }
   
   
    this.affbeforelt = function()
    {
        var ecrant = elt;
        ecrant.snapshotItem(0).innerHTML = '
'+this.tostring()+' '+ecrant.snapshotItem(0).innerHTML ;
    }
   
    this.tostring = function()
    {
        return this.h+" heurs "+ this.m + " minute "+this.s+" second";
    }
   
}
function str2int(str) {
    if ( str.replace ) // If param is a string
        return parseInt(str.replace(/\./g,'')) ; // Removes dots, parses as int
    else // If param isn't a string
        return str ; // Hope it's an int
}

1 réponse

PetoleTeam Messages postés 3426 Date d'inscription lundi 26 décembre 2005 Statut Membre Dernière intervention 14 janvier 2011 16
18 févr. 2009 à 17:03
Bonjour,
un petit tour du coté de SETINTERVAL
SETTIMEOUT CLASS OBJET

avec notamment l'utilisation de la fonction
//-- return la reference de la
fonction fct_ de l'objet o_
function Get_RefFonction( o_, fct_){

  return( function(){o_[ fct_]()});
}
bonne lecture...
;O)
0
Rejoignez-nous