I'm not a javascript expert by any stretch, but your example would run the script each time the page was loaded, including the first, which is technically not a refresh.
What I would do is check for a variable. If it's not present, set it as a hidden input variable on the form. If it is there, then you can be reasonably sure that the page was refreshed.
ex: if (!isset($chk_var)) {
do your thing...
$chk_var = "yes";
}
in html: <input type="hidden" name="chk_var" value="<?= $chk_var ?>">