0

Is there a way to make an action available only at a specific time that was programmed in advance in HTML?

For example: A radio player that will only work at a certain time and at other times the link is not active

Adi
  • 5,089
  • 6
  • 33
  • 47

2 Answers2

1

Yes, it is possible using JavaScript. Look at functions setTimeout or setInterval. You can find an example here - Call a javascript function at a specific time of day .

If for some reason you cannot use JavaScrip, another option would be to use some server-side technology (PHP, ASP.Net) to modify the html before the response is returned to the browser. In this case, the back-end should check if the current time is valid to let the user click the link and modify the Html markup such that e. g. a link would be clickable or not.

Community
  • 1
  • 1
Donatas Bacius
  • 646
  • 6
  • 16
1

Yes, use javascript to do this. Many ways:

1st method - Link controlled by javascript function

<script>
function is_active() {
  if (/* working at this time */) return true;
  else return false;
}
</script>
<a href="..." onClick="return is_active();">...</a>

This is probably the easiest way, as long as the functionality is controlled by a link

2nd method - put a box on top of the player so it can't be clicked

<div style="position:relative;">
player here - including links to start/stop
<div id="overlaydiv"
 style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;
        opacity: 50%; background-color: white;">&nbsp;</div>
</div>
<script>
setTimeout(function () {
  $(overlaydiv).css("display: block;")
},milliseconds until inactive);
</script>

Be careful if security is important

If you use javascript to get the local time on the user's computer, be aware that a user may MANIPULATE their clock. So you may not be able to trust it.

You can mitigate this risk by getting the server to save the actual time in a javascript variable on page load. However, the user can still manipulate their clock if you are using timers to find the time change.

If it is important, you should probably do an AJAX request to check the server time before allowing a link click to start the player (eg. with the 1st method).

ronalchn
  • 12,225
  • 10
  • 51
  • 61