如何在特定时间显示按钮(比如上午10点) [英] How to make a button visible at a particular time (say 10 am)

查看:121
本文介绍了如何在特定时间显示按钮(比如上午10点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在购物网站上工作,我需要在特定时间(例如早上10点)看到购买按钮。

我知道使用计时器来做,但是可以用javascript或其他方法做同样的事情。



提前谢谢。

Hi Everyone

I am working on a shopping website where I need to make the buy button visible at a particular time (say 10 in the morning).
I know to do it using timer, but is it possible to do the same with javascript or some other method.

Thanks in advance.

推荐答案

请参阅我对该问题的评论。这就是您所需要的:

http://www.w3schools.com/jsref/met_win_settimeout。 asp [ ^ ],

http://www.w3schools.com/js/js_dates.asp [ ^ ]。



根据当前系统时间,您需要重新计算时间的一个小问题(请参阅上面的链接)。您可以在每次加载页面时以及处理上一个计时器事件时执行此操作。我也希望你明白,只有在浏览器运行并且加载了相应的网页时它才会起作用。 :-)



可见性最好由元素样式控制,如下例所示: http://stackoverflow.com/questions/6205148/changing-visibility-using-javascript [ ^ ](见答案7,2)。



有关所有可见性选项值,请参阅: http://www.w3schools.com/jsref/prop_style_visibility.asp [ ^ ]。



-SA
Please see my comment to the question. This is all you need:
http://www.w3schools.com/jsref/met_win_settimeout.asp[^],
http://www.w3schools.com/js/js_dates.asp[^].

One little problem that you will need to recalculate time based on the current system time (see the link above). You can do it every time you load the page and also when you handle the previous timer event. I also hope you understand that it will only work if the browser is running and the appropriate Web page is loaded. :-)

Visibility is best controlled by the element style, like in this example: http://stackoverflow.com/questions/6205148/changing-visibility-using-javascript[^] (see answers 7, 2).

For all visibility option values, please see: http://www.w3schools.com/jsref/prop_style_visibility.asp[^].

—SA


1)您可以在javascript中创建计时器,检查时间并显示按钮。这很简单:

1) You can make a timer in javascript, check time and show the button. It is quite simple:
<script>
var myVar = setInterval(function () {myTimer()}, 1000);
var target = new Date(2015, 00, 14, 20, 33, 00);

function myTimer() {
    var d = new Date();
    if(d > target){
        document.getElementById("buy").style.visibility = "visible";
        clearTimeout(myVar);
    }
}
</script>
<button id="buy" style="visibility:hidden;">Buy now!</button>





但是:

2)哪个时间?谁的时间?当地时间将与服务器的时间不同,可以简单地更改。

3)如果有人查看发出的代码,他会看到,在特定时间发生了什么,并且可以简单地调用它。

3)如果我知道,有一个按钮我可以在notime中显示它。



所以,你最好不要隐藏它只是,禁用它。但也要确保在服务器端检查操作是否合法。由于您无法信任客户端的时钟,您最好在客户端实现自己的时钟或倒计时以触发启用按钮。



But:
2) Which time? Who's time? Local time will differ from your server's time, and can be simply altered.
3) If somebody looks at the emitted code, he will see, what happens on that specific time, and can simply call it.
3) If I know, that there is a button I can make it visible in notime.

So, you better not hide it just, disable it. But also make sure, to check on server side if the action is legitimate or not. As you can not trust the client's clock, you better implement your own "clock" or countdown on client side to trigger enabling button.


这篇关于如何在特定时间显示按钮(比如上午10点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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