I'm starting with JavaScript and Ajax and I need a little help pls. I have two Ajax functions that load HTML to an assigned div, inside there is a second counter that refreshes the Ajax function (it works!) But when I give the button to pass to the other Ajax function I keep the previous counter running and I it refreshes the function of before and the new one (go lioo!) I leave the code to you here
<script>
function loadactivity () {
\$.post("http://miweb/ajax.cgi",
{
'action': "activity",
'region': "$region",
'bracket': "$bracket"
},
function(data){
\$("#activity").html(data);
}
);
}
function loadtracking () {
\$.post("http://miweb/ajax.cgi",
{
'action': "tracking",
'region': "$region",
'bracket': "$bracket"
},
function(data){
\$("#activity").html(data);
}
);
}
function countdown(action, seconds) {
if (action == "stop") {
clearInterval(timerId);
}
else {
var count = seconds;
var timerId = setInterval(function() {
count--;
document.getElementById("countdowntimer").textContent = count;
if(count == 0) {
clearInterval(timerId);
if (action == "activity") {
loadactivity();
}
if (action == "tracking") {
loadtracking();
}
}
}, 1000);
}
}
loadactivity();
</script>
In the Ajax functions, the following goes:
<script>
countdown("stop");
countdown("activity", 30);
</script>