var btwait = false; function doAction(item){ action = item[0]; if (action == 'create'){ btCreatePixel(item); } if (action == 'register'){ btRegister(item); } if (action == 'login'){ btLogin(item); } if (action == 'send'){ btSend(item); } } function btCreatePixel(item){ tid = item[1]; window.btid = tid; createImage('pcreate'); } function btSend(item){ a = document.getElementById('btpix'); rh = Math.random(); params = false; if (item.length > 1){ params = item[1]; } createImage('psend', params); } function btRegister(item){ email = item[1]; first_name = item[2]; last_name = item[3]; obj = {email: email, first_name: first_name, last_name: last_name}; createImage('pregister', obj); } function btLogin(item){ email = item[1]; obj = {email: email}; createImage('plogin', obj); } function createImage(pname, params){ tid = window.btid; rh = Math.random(); url = 'https://bt.hosting.bridgeagency.pl/'+pname+'-'+tid+'.jpg?r='+rh; if (params){ enc = JSON.stringify(params); enc = encodeURIComponent(enc); url += '¶ms='+enc; } var downloadingImage = new Image(); downloadingImage.src = url; downloadingImage.onload = function(){ a=document.createElement('img'); a.src = url; a.id = 'btpix'; btwait = false; } } var btactions = window.bt.q; var timer = setInterval(function() { if (btwait == false){ if (btactions.length > 0){ item = btactions[0]; btwait = true; doAction(item); console.log(item); btactions.splice(0,1); console.log(btactions); } } }, 100, btactions);