I've inherited the task of creating a web page for my dept. application on the intranet. My experience with javascript is limited and would rather do this via PHP (v 4.3 at work), but javascript is what I've to work with.

Once a week we run migration (Oracle) at a specific time. What I'm trying to do is disable the links on the page when said time is reached. I already have a function that can toggle between the two, however a new page appears when it's triggered.

Is there any way possible to toggle on the same page?

Here is my test code...

(function() {

    var state = null;

    (function loop() {
        var now = new Date();
        var day = now.getDay();
        var min = 60 * now.getHour() + now.getMinutes();

        var disable = (day === 3 && (min >= 600 && min < 630));
        if (disable !== state) {
             if (disable) {
                 // call your disable code
             } else {
                 // call your enable code
             }
        }
        state = disable;
        setTimeout(loop, 1000);
    })();

})();
Member Avatar for diafol

The php is straightforward. Something like:

$comp = date('w-H-i');
list($day,$h,$i) = explode('-',$comp);
$mins = intval($h)*60 + intval($i);
if($day == 3 && $mins >= 600 && $mins <= 630){

}else{

}

However, you don't get the 'update' from the loop. So links are visible even though time is between 600 and 630 on a 'stale' page.

You can wrap the links in a div or add a class to each 'togglable' link or parent tag:

<ul class="toggle-link">...</ul>


if (disable) {
    $('.toggle-link').hide();
} else {
    $('.toggle-link').show();
}    

Obviously that's with jQuery, but you can change it to vanilla js easily enough. Is that what you need? I'm slightly confused with what's causing the page refresh.

diafol, thanks for responding. I'm using javascript. I'm using doc. getElement(....).style.visible to toggle by id but getting objecdefined error. the logic works, but dont know the cause. I will post code when i get in later. Using onload to call it..

Member Avatar for diafol

BTW setInterval may be better:

setInterval(function(){
    //test and refresh page area if disable var changes
},60000); //test every minute

LOL .. slightly ahead of you as I had changed it to use setInterval the other day :) ... code is listed below. I'm receiving a "object required" error message. Would making a global variable of the objects help in this case? They're not buttons, but href links instead.

<script language="javascript">
var myInterval; 

function page_load() {

   myInterval = setInterval(function(){ShowLink()},60000);
   }

function ShowLink() {
        var now = new Date();
        var clock = now.toTimeString();
        var nStart = 923;
        var nExpired = 924;
        var MigTime = 60 * now.getHours() + now.getMinutes();

        var disable = (day === 0 && (MigTime >= nStart && MigTime < nExpired));

        if (disable == true) {
                //  hide link
                document.getElementById("prdlnk").style.visibility = "hidden";
                document.getElementById("viewlnk").style.visibility = "hidden";
                document.getElementById("MigMsg").innerHTML= "Scheduled Migration in Progress. Please try later.";

             }

         if (MigTime > nExpired) {

                // show link...
               document.getElementById("prdlnk").style.visibility = "visible";
               document.getElementById("viewlnk").style.visibility = "visible";
               document.getElementById("MigMsg").innerHTML= "";

               // clear the interval since migration period has expired.
               clearInterval(myInterval);

             }

            }

</script>

problem resolved. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.