2016-04-22 13 views
0

Ich versuche, JavaScript zu lernen und stieß auf eine Wand aufgrund der Arbeit mit Variablen durch mehrere Funktionen.Javascript globale Variable einfache Lösung

Nach dem Durchsuchen der Dokumentation und stackoverflow die meisten gewählten Antworten, im Zusammenhang mit globalen Variablen und wie Sie effektiv mit ihnen durch mehrere Funktionen arbeiten.

Ich fand wenig bis keine einfache Lösung.

Jede Art von Seele in der Lage zu korrigieren, wie dieses komplette Stück Code richtig funktionieren kann?

Variable „ttl“ und „msg“ nötig sind, um von „Ajax“ Funktion übergeben werden, um „mitteilen“ Funktion

<script src="js/jquery.js"></script> 
<script src="js/bootstrap-notify.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     // Global variables ttl and msg needed ? 
     var ttl = null; 
     var msg = null; 

     // Make an ajax call to json file. 
     $.ajax({ 
      url: "lastorder.json", 
      dataType: "json", 
      // Parse title and message from json. 
      success: function(data) { 
       ttl = data.title; 
       msg = data.message; 
      } 
     }); 
     // Notify function displays a title and message that was parsed from json file 
     $.notify({ 
      icon: 'img/avatar.jpg', 
      title: ttl, 
      message: msg 
     },{ 
      type: 'minimalist', 
      delay: 3000, 
      icon_type: 'image', 
      template: '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">' + 
       '<img data-notify="icon" class="img-circle pull-left">' + 
       '<span data-notify="title">{1}</span>' + 
       '<span data-notify="message">{2}</span>' + 
      '</div>' 
     }); 

    }); 
</script> 
+0

Können Sie bewegen Sie Ihren '$ .notify' Code in das' success' Rückruf? Oder gibt es einen Grund, warum es außerhalb sein muss? –

+0

erhalten Sie Nullwerte für beide Variablen in der Benachrichtigungsfunktion? –

+0

Mike, das funktioniert, aber ich kann die Funktion nicht innerhalb einer Funktion verschachteln, wenn ich weitere Dinge hinzufügen werde. Ich habe das [Referenz] (http://stackoverflow.com/questions/9085839/surprised-that-global-variable-has-undefined-value-in-javascript) überprüft, da ich die ttl und msg auf mehr als verwenden müssen nur '$ .notify' Funktion, so ist es wichtig, eine konkrete Lösung für die Verwendung der Variablen zu haben. –

Antwort

0

Wenn Sie Antwort zurückgegeben werden soll von Ajax-Anforderung, dann können Sie Erhalte diese Daten, nachdem die ajax-Anfrage erfolgreich ausgeführt wurde. Da Ajax asynchronus-Aufruf ist, wird der Code, der auf die Antwort wartet, bereits zu dem Zeitpunkt ausgeführt, zu dem die Antwort empfangen wird. So erhalten Sie keine genaue Rückgabewert von Ajax-Antwort in Benachrichtigungsfunktion.

Also, um die Werte von Ajax Antwort zurückgeben, müssen Sie notify Funktion in Seite success() Methode von Ajax aufrufen.

refer this SO question

1

Wenn Sie @MikeC Lösung versuchen:

$(document).ready(function() { 
     // Global variables ttl and msg needed ? 
     var ttl = null; 
     var msg = null; 

     // Notify function displays a title and message that was parsed from json file 
     function notify(data) { 

      ttl = data.title; 
      msg = data.message; 

      $.notify({ 
       icon: 'img/avatar.jpg', 
       title: ttl, 
       message: msg 
      }, { 
       type: 'minimalist', 
       delay: 3000, 
       icon_type: 'image', 
       template: '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">' + 
       '<img data-notify="icon" class="img-circle pull-left">' + 
       '<span data-notify="title">{1}</span>' + 
       '<span data-notify="message">{2}</span>' + 
      '</div>' 
      }); 

     } 

     // Make an ajax call to json file. 
     $.ajax({ 
      url: "lastorder.json", 
      dataType: "json", 
      // Parse title and message from json. 
      success: notify 
     }); 

    });