Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jan-15 14:36pm    
"Using timer" and "with Javascript" is the same thing. What's the problem?
—SA
JPais 15-Jan-15 12:51pm    
oh sorry by timer i meant asp timer comtrol.
Sergey Alexandrovich Kryukov 15-Jan-15 13:23pm    
Nothing to sorry about, not problem. Didn't I answer your question?
—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
 
Share this answer
 
v2
1) You can make a timer in javascript, check time and show the button. It is quite simple:
XML
<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>


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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Jan-15 14:51pm    
Not quite so. You should first get current and then calculate interval needed to invoke the event at 10 am. Please see Solution 1.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900