I'm trying to learn javascript and encountered a wall due to working with variables through multiple functions.
After browsing through the documentation and stackoverflow most voted answers, related to global variables and how to effectively work with them through multiple functions.
I found little to no simple solution.
Any kind soul able to correct how this complete piece of code can work properly ?
variable "ttl" and "msg" are needed to be passed from "ajax" function to "notify" function
<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>