I have a global variable numberOfMessages
that I want to immediately set to a particular number according to what a call to a solidity contract brings back. The call is made in the document.ready function when the page is loaded. However, the variable isn't changed outside this function.
My code is basically like this:
var numberOfMessages = 0 // declared outside any function, so should be global
$(document).ready(function () {
Message.deployed().then(function (contractInstance) {
contractInstance.getNumberMessages.call().then(function (v) {
numberOfMessages = v
alert(numberOfMessages) // returns something other than 0
})
})
})
alert(numberOfMessages) // returns 0
How can I have a global variable that is set to what a function returns when the page is loaded?