2016-04-22 3 views
0

私はjavascriptを学ぼうとしていて、複数の機能を使って変数を扱うため、壁に遭遇しました。JavaScriptのグローバル変数シンプルなソリューション

グローバル変数に関連するほとんどの投票回答と、複数の機能を通じて効果的にそれらを操作する方法については、ドキュメントとstackoverflowを参照してください。

私はほとんど、まったく単純な解決策を見出しませんでした。

この完全なコードが適切に動作する方法を修正することができますか?

変数「TTL」と「MSGは、」あなたはあなたができる、その後、AJAX要求から返された応答にしたい場合は、「通知」する「AJAX」機能機能

<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

を、あなたは 'SUCCESS'コールバックにあなたの' $ .notify'コードを移動することはできますか?それとも外に出なければならない理由がありますか? –

+0

あなたはnotify関数の両方の変数にnull値を取得していますか? –

+0

マイク、それは動作しますが、私は関数の中で関数を入れ子にすることはできません。私はこれを確認しました[参考](http://stackoverflow.com/questions/9085839/surprised-that-global-variable-has-undefined-value-in-javascript)私はttlとmsg以上を使用する必要があるので'$ .notify'関数だけなので、変数を使うための具体的な解決方法が重要です。 –

答えて

0

から渡されることを必要としていますajaxリクエストが正常に実行された後にこのデータを取得します。 ajaxは非同期呼び出しなので、応答を待っているコードは、応答が受信されるまでにすでに実行されています。 したがって、notify関数でのajaxレスポンスによる正確な値の戻りは得られません。

したがって、ajaxレスポンスで返される値を使用するには、success() ajaxメソッドのnotify関数を呼び出す必要があります。

refer this SO question

1

あなたは@MikeC解決しようとする場合:

$(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 
     }); 

    }); 
関連する問題