最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to automatically reload page after a certain time based on a php variable - Stack Overflow

programmeradmin1浏览0评论

I currently have a php file that automatically plays the song at the top of a playlist. I have a $duration value that I pull from a mysql database and I want the page to automatically refresh after the song is finished playing.

The song is currently played within an iframe so this is the only way to tell that the song has approximately finished.

I've tried doing this in javascript but I'm having issues trying to pass a variable to the timeout period.

<script type="text/JavaScript">
    function timedRefresh(timeoutPeriod{
        setTimeout("location.reload(true);",timeoutPeriod);
    }
</script>

<body onload="JavaScript:timedRefresh($x);">
    ...
</body>

I currently have a php file that automatically plays the song at the top of a playlist. I have a $duration value that I pull from a mysql database and I want the page to automatically refresh after the song is finished playing.

The song is currently played within an iframe so this is the only way to tell that the song has approximately finished.

I've tried doing this in javascript but I'm having issues trying to pass a variable to the timeout period.

<script type="text/JavaScript">
    function timedRefresh(timeoutPeriod{
        setTimeout("location.reload(true);",timeoutPeriod);
    }
</script>

<body onload="JavaScript:timedRefresh($x);">
    ...
</body>
Share Improve this question edited Dec 6, 2012 at 4:53 Matt Clark 28.6k20 gold badges77 silver badges125 bronze badges asked Dec 6, 2012 at 4:38 user448948user448948 11 silver badge3 bronze badges 1
  • codeforbrowser./blog/lesser-known-facts-about-html – defau1t Commented Dec 6, 2012 at 4:44
Add a ment  | 

2 Answers 2

Reset to default 3

It is simply a straightforward PHP variable output into the source:

<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod){
    setTimeout("location.reload(true);",<?php echo $song_duration_in_milliseconds; ?>);
}
</script>

Or use meta-refresh as pointed out in @Matt Clark's answer.

You can also use a META tag for refresh.

In PHP:

<?php
    $Refresh = 600;
?>

And in your HTML head you can modify a META Tag:

<meta http-equiv="refresh" content="<?= $Refresh ?>">

Or as stated by Amadan you can just use the PHP to specify the meta tag in your PHP:

header("Refresh: " . $Refresh);

Or In Javascript:

<script type="text/JavaScript">
    setTimeout("location.reload(true);",<?= $Refresh ?>);
</script>

Side Notes

The short tags:

<?= $Variable, $Variable2 ?>

Are the equivalent of using:

<?php echo $Variable; echo $Variable2; ?>
发布评论

评论列表(0)

  1. 暂无评论