0

This should be simple, but doesn't work

Essentially I want to get a return result and reload my page if the answer is 'yes'

Here is the timer.php

<?php
    $filename = 'timer.tmr';

    if (file_exists($filename)) {
        unlink($filename);
        echo 'yes';
    } else {
        echo 'no';
    }
?>

and here is the script in my main file

var myVar = setInterval(function () {myTimer()}, 5000);
function myTimer() {
     sendto =  "timer.php";
     $.get(sendto, function(data, status) {
         if (data === 'yes') {
            location.reload();
         }
     });
}

Basically in the timer.php

If the file exists I delete it and return a 'yes'

But the jquery doesn't do the reload function, if I check the data with an alert(data), it returns a 'yes' So I just don't understand the problem.

Karter
  • 21
  • 2

4 Answers4

2

If you are getting into the if condition, then you should try window.location.href = window.location.href; instead of location.reload();

and If you're not getting into the if condition, change condition with if ($.trim(data) === "yes")


thanks to Shaunak Shukla (again thank you)

here is the final solution

     var myVar = setInterval(function () {myTimer()}, 10000);
     function myTimer() {
     sendto =  "timer.php";
     $.get(sendto, function(data, status) {
     if ($.trim(data) === "yes"){
       location.reload();
       }
      });

       }

it needed the trim(data) must have had something in it..

Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
Karter
  • 21
  • 2
0

Use json_encode in the echos and improve your checking:

<?php
$filename = 'timer.tmr';

if (file_exists($filename)) {

    unlink($filename);
    if (!file_exists($filename))
       echo json_encode('yes');
    else
       echo json_encode('no');
} else {

    echo json_encode('no');
}

?>

And change the reload to a more direct scope:

 var myVar = setInterval(function () {myTimer()}, 5000);
 function myTimer() {

 sendto =  "timer.php";
 $.get(sendto, function(data, status) {
   if (data === 'yes'){
    window.location.reload();
  }
  });

  }
capcj
  • 1,535
  • 1
  • 16
  • 23
0

Let complete ajax request then try to reload :

setTimeout(function(){// wait for 5 secs(2)
        location.reload(); // then reload the page.(3)
}, 5000);
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

I would Suggest, please check the Type casting (data type). I find, you have used === , it will check string to string or int to int.