私は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>
を、あなたは 'SUCCESS'コールバックにあなたの' $ .notify'コードを移動することはできますか?それとも外に出なければならない理由がありますか? –
あなたはnotify関数の両方の変数にnull値を取得していますか? –
マイク、それは動作しますが、私は関数の中で関数を入れ子にすることはできません。私はこれを確認しました[参考](http://stackoverflow.com/questions/9085839/surprised-that-global-variable-has-undefined-value-in-javascript)私はttlとmsg以上を使用する必要があるので'$ .notify'関数だけなので、変数を使うための具体的な解決方法が重要です。 –