2017-01-17 3 views

Répondre

0

d'abord créer variable globale la manière correcte:

var global_variable 

que les fonctions:

function getInfo() 
{ 
    global_variable = 3; 
} 

function getDuration() 
{ 
    console.log(global_variable); 
} 

et que exécuter les fonctions:

getDuration(); // Here you get variable is undefined, instead of not defined error 
getInfo(); 
getDuration(); // Here you will get 3; 

jsfiddle

+0

https://github.com/MANTENN/Days4God/blob/master/playergenerator.html Pourquoi ça ne marche pas ici? ligne 154? –

+0

'var runs = links.length-1; function appendAudio (n) { \t/* var s'exécute; */'vous ré-attribuez' runs' à l'intérieur de la fonction. – kirillrocks

+0

Je l'ai supprimé, maintenant il s'exécute à l'infini. parce qu'il ne peut pas obtenir la longueur des liens –

0

Vous devez d'abord définir votre variable, sinon il va imprimer 'undefined'.

+0

Oui, je sais que, mais je ne vais pas appeler getDuration tout de suite. Le problème est une fois que j'appelle getInfo puis getDuration() il est encore indéfini. –

0
// Define global_variable. 
    var global_variable; 

    // optionally you can initialize the global variable like below 
    // var global_variable = 0; 
    // Or global_variable = 0; 

    function getInfo() 
    { 
     global_variable = 3; 
    } 
    function getDuration() 
    { 
     getInfo(); 
     alert(global_variable); 
    } 

    // test invoke 
    getDuration(); 
0

Basé sur la façon dont le code est exécuté. La variable "Length" sera 0.

Avec lui et basé sur ma fonction récursive, il était en boucle.