var Form=$("#user_balance");
var alias=$('.item-site:first').parent().find(".item-alias").text();
var first_token;
var current_token;
$('.infostop:first').prepend('<li><button id="stop" type="button" class="btn btn-danger" style="height:50px;width:80px;">Stop</button></li>');
$('.infostop:first').prepend('<li><button id="start" type="button" class="btn btn-primary" style="height:50px;width:80px;">Start</button></li>');
$('#start').click(function(){
flag=1;
run();
});
$('#stop').click(function(){
flag=0;
});
function run(){
var interval_obj = setInterval(function(){
$.ajax({
type:"POST",
url:Form.attr('action'),
data:Form.serialize(),
async:true,
success:function(result,status,xhr){
if(result.status==='success'){
first_token=current_token;
current_token=result.token;
if(first_token!=current_token)
{
var request='http://adiphy.com/links/go?callback=fn_Pay&method=POST&token='+current_token+'&alias='+alias;
$.get(request, function(data){
get_user_balance();
})
}
}
},
error:function(xhr,status,error){
console.log("An error occured: "+xhr.responseText);
}
})
if(flag===0)
clearInterval(interval_obj);
}, 2000);
}
function get_user_balance(){
var Form=$("#user_balance");
$.ajax({
type:"POST",
url:Form.attr('action'),
data:Form.serialize(),
async:true,
success:function(result,status,xhr){
if(result.status==='success'){
$('span.user_balance').html(result.balance);
$('span.h-item-value').html(result.clicks);
$('#user-token').html(result.token);
}
},
error:function(xhr,status,error){
console.log("An error occured: "+xhr.responseText);
}
});
}