Yes possible using setInterval
1) Keep your ajax within a function.
function fun() {
$.ajax({
type: 'POST',
url: 'increment.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#hidden').val(data);// first set the value
}
});
}
2) Now using setInterval, you can execute it every second.
var interval = setInterval(fun, 1000);
3) To pause/clear use clearInterval
clearInterval(interval);
4) As other pointed that using setInterval for ajax is not wise, so try using
var interval;
function callAjax() {
$.ajax({
type: 'POST',
url: 'increment.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#hidden').val(data);// first set the value
interval = setTimeout(callAjax, 1000);
}
});
}
callAjax();
//clearTimeout(interval);