You have to declare variable outside. like
var timerId = 0;
and then put your code
var timerId= 0;
$('#apply').click(function () {
if ($('#autoupdate').prop("checked")) {
timerId = setInterval(function () {
$.ajax({
type: "post",
url: "1.php",
cache: false,
success: function (html) {
$("#result").html(html);
}
});
}, 1000);
} else {clearInterval(timerId);}
});
cause if it's declared inside if
then accessing variable timerId from else will not be possible, cause its undefined.