完成后,如何创建倒数计时器并使用PHP运行SQL查询? [英] How to create a countdown timer and run a sql query with PHP when it is completed?

查看:102
本文介绍了完成后,如何创建倒数计时器并使用PHP运行SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下jQuery脚本来倒计时直到$end的时间,并在发生这种情况时将其显示为完成.

I am using the following jQuery script that countdowns the time until the $end and when this happen it displays completed.

但是,当您更改计算机上的时间时,倒数计时也会更改.我的想法就像Groupon的倒数计时一样.

However when you change the time on your computer, the countdown is changed. What I had in mind is like those countdowns that Groupon has.

我的问题是如何更改无法更改的时间源,例如服务器的时间?当计时器达到0时,如何使用PHP运行SQL查询?

My question is how to change the time source in something that can not be changed, maybe the server's time? And when the timer reaches 0, how can I run a SQL query using PHP?

我是完全错误的方式吗?

Am I totally in the wrong way?

谢谢.

<?php $end = 'september 1, 2011 19:24'; ?>

<script type="text/javascript">
$(document).ready(function() {
    $("#time").countdown({
        date: "<?=$end?>",
        onComplete: function( event ){
            $(this).html("<i>Completed</i>");
        },
        leadingZero: true
    });
});
</script>

<p id="time" class="time"></p>

推荐答案

在大多数情况下可以使用JavaScript倒数计时器,但是您不能依靠客户端脚本不加速"计时器.用户始终可以通过更改系统时钟,手动弄乱JavaScript变量等等来欺骗计时器.

It's ok to have a JavaScript countdown timer in most situations, but you can't rely on a client-side script not to "speed up" the timer. The user can always cheat the timer by changing the system clock, messing with the JavaScript variables manually, and so on.

在行与行之间阅读时,似乎想要做一些事情,例如在产品上市或报价过期时在数据库中添加注释.如果是这样,您应该绝对在服务器端执行此操作,尤其是即使用户离开页面导航并且可能仅在截止日期之后返回时也必须运行查询的情况下.也许可以完全重新考虑逻辑:不要在计时器用完时在数据库中做笔记,而应在数据库中预先放置一个过期时间,以标记更改何时生效.

Reading between the lines it seems like you want to do something like put a note in the database when a product comes available or an offer expires. If so, you should absolutely do this server-side, especially if the query must be run even if the user navigates away from the page and maybe returns only after the deadline. Maybe re-think the logic completely: instead of making a note in the database at the time the timer runs out, put the expiration time in the database beforehand that marks when the change comes into effect.

另一种可能性是,当JavaScript计时器达到目标时,页面将调用服务器.然后,服务器检查呼叫是否在正确的时间进行.如果是这样,请运行SQL查询,否则请告诉JavaScript它应保持计时器运行. (例如,如果服务器认为呼叫提前了10分钟,则将数据返回给JavaScript,要求将计时器重置为10分钟,然后重试.)

Another possibility is when the JavaScript timer hits the mark have the page make a call to the server. The server then checks if the call has been made at the correct time. If so, run the SQL query, but otherwise tell JavaScript that it should keep the timer running. (For example if the server thinks the call was made 10 minutes early, return data to JavaScript saying that it should reset the timer to 10 minutes and then try again.)

这篇关于完成后,如何创建倒数计时器并使用PHP运行SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆