dongyimeng3764 2012-01-19 00:57
浏览 24

php倒计时服务器端

i have a question about a javascript/php cron issue. well its not an issue per se but in my case it is. i have the below javascript that sets a cookie and counts down till the end of that cookie's time. in my case its 10 minutes. I also have a cron file that runs every 10 minutes. I would like to synchronize the countdown with the cron file.

The below script works well when i call it from the page however it counts down from the time the user visits the page and its out of sync with the cron file im running.

i understand this is not a php question but the pages im displaying the scripts are php.

Any pointers for me? Help would be greatly appreciated. Thank you for reading.

All i have is below.

<div id="countre4">
  <script type="text/javascript">
 function setCookie(c_name, value)
    {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 14);

        var c_value = escape(value) + "; expires=" + exdate.toUTCString();
        document.cookie = c_name + "=" + c_value;
    }

    function mycountre(countdownId, countdownTarget, countdownSeconds, countdownLooping){
        var countre = document.getElementById(countdownId); // get html element
        if (!countre) {
            return;
        }

        var target = countdownTarget; // target time
        var intervalId; // id of the interval

        setCookie("time", target);

        // update function
        function updatecountre(){
            var time = Math.floor((target - new Date().getTime()) / 1000); // countdown time in seconds
            if (time < 0) { // if countdown ends
                if (countdownLooping) { // if it should loop
                    target += 1000 * countdownSeconds; // set new target time
                    time = Math.floor((target - new Date().getTime()) / 1000); // recalculate current time

                    setCookie("time", target);
                } else { // otherwise
                    clearInterval(intervalId); // clear interval
                    time = 0; // set time to 0 to avoid displaying negative values
                }
            }

            // split time to seconds, minutes and hours
            var seconds = '0' + (time % 60);
            time = (time - seconds) / 60;
            var minutes = '0' + (time % 60);
            time = (time - minutes) / 60;
            var hours = '0' + time;

            // make string from splited values
            var str = hours.substring(hours.length - 2) + ':' + minutes.substring(minutes.length - 2) + ':' + seconds.substring(seconds.length - 2);
            countre.innerHTML = str;
        }

        intervalId = setInterval(updatecountre, 200); // start interval to execute update function periodically
    };

    var parts = document.cookie.split(';');
    var cookie = new Date().getTime() + 1000 * 15 * 60;

    for (var i = 0; i < parts.length; ++i) {
        if (parts[i].trim().indexOf('time') == 0) {
            var value = parts[i].trim().split('=');
            cookie = parseInt(value[1]);
        }
    }

    mycountre(
        'countre4', // id of the html element
        cookie,
        10 * 60, // time in seconds (10min here)
        true // loop after countdown ends?
    );


</script>
</div>
  • 写回答

1条回答 默认 最新

  • dppfxf909679 2012-01-19 01:05
    关注

    Maybe you should rethink your approach. Don't use a cookie for a piece of information actually stored on the server.

    How about instead of a cookie, each PHP page simply set a global JS variable for how much time you have left. Something sorta like this:

    <script>
      var timeLeft = <? echo $timeLeftInMilliseconds ?>;
      startCounter(timeleft);
    </script>
    

    Then your counter script can read that and animate a counter accordingly. This leaves it up to your server side code to accurately deliver when the next cron will happen.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?