data = { gold: 0, mine: 0, miners: 0, level: 1 } function sendRequest(url, params, callback) { var xhr = new XMLHttpRequest(); xhr.open("POST", url); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { callback(xhr); } } xhr.send(params); } function updateData() { for (var i = 0; i < arguments.length; i++) { document.getElementById(arguments[i]).innerHTML = data[arguments[i]]; } } function withdrawMine() { sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) { var gold = parseInt(xhr.responseText); // Server's response is a string if(isNaN(gold)) return; data.gold = gold; data.mine = 0; updateData("gold", "mine"); }) } function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { var ret = xhr.responseText; data.gold = parseInt(ret); // Server's response is a string updateData("gold"); }) } function updateMine() { data.mine += (data.miners+1); updateData("mine"); } function init() { initCraftMine(); window.setInterval(updateMine, 1000); }