I am experiencing an issue with my AJAX call as it keeps triggering my controller repeatedly.
AJAX function
<script type="text/javascript>
var stopTime =0;
var scoreCheck = function ()
{
$.ajax({
url: "<?php echo 'http://127.0.0.1/ProgVsProg/main/checkScore'?>",
success:function(output){
if(output){
$('#maincontent1').html(output);
clearTimeout(stopTime);
}
else{
stopTime = setTimeout(scoreCheck, 1000);
}
}
});
}
stopTime = setTimeout(scoreCheck,3000);
</script>
Controller
public function checkScore(){
$id = $this->session->userdata('userID');
$battleID = $this->lawmodel->getBattle($id);
foreach($battleID as $row){
$Rscore = $row->requestedScore;
$Cscore = $row->challengerScore;
if($Cscore=="1"){
redirect('main/secondRound');
}
else if($Rscore == '1'){
redirect('main/secondRound');
}
}
}
Here is my secondRound function
public function secondRound(){
$category = 'medium';
$data['results'] = $this->lawmodel->roundTwoQuestion($category);
$data['content'] = 'battlePage';
if($this->session->userdata('is_logged_in'))
{
$this->load->view('includes/template2',$data);
}
else
{
redirect('main/restricted');
}
}
The challenge I'm facing is how to halt the AJAX call after the initial redirect. I have set up a setTimeout function to continuously check for changes in the database and redirect to another controller upon detection of change. In my model roundTwoQuestion(), a random database entry is queried with a limit of 1.
In my view, it constantly redirects and refreshes with new data from roundTwoQuestion. Any assistance would be greatly appreciated as I am relatively new to working with AJAX. :(