3
\$\begingroup\$

I'm working on something for my school's laptops that will disable videos/games between 8:00 AM and 3:15 PM. Due to the giant time gap, I will not be waiting to see if the code functions properly or not. What I want to know is if my code will function properly, as I'm not good enough at JavaScript to know myself.

function timer() {
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();

    if (minutes < 10) {
        var minutes = "0" + minutes;
    } else {}

    $(".time").html(hours + ":" + minutes),
        setInterval(timer, 1000);

    // Disable stuff
    if (hours > 8 & hours < 15) {
        $("video").hide();
        $("video").parent().html("<b>Can you do us all a favor and not eat up all the bandwidth with your video watching?</b>" + "<br/>" + "<b>Thank you!</b>" + "<br/>" + "<br/>");
        $("embed").hide();
        $("embed").parent().html("<b>This is no time for childish games.</b>" + "<br/>" + "<b>Shouldn't you be studying or doing something related to school?</b>" + "<br/>" + "<br/>");
    } else if(hours > 15 & hours < 8) {
        $("video").show();
        $("embed").show();
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ In your first if statement, you put the semi-colon at the end of the else. I mean it works, but it's just really strange looking and looks like an error to anyone besides yourself. I don't see a reason to not just place it at the end of the line it's intended for. Also, it goes against the seemingly apparent convention that you have setup throughout your code. \$\endgroup\$ Apr 13, 2015 at 17:41
  • 1
    \$\begingroup\$ @EvanBechtol Thanks for the help. I think it looks strange now that you mention it. \$\endgroup\$
    – thrifus
    Apr 13, 2015 at 17:58
  • \$\begingroup\$ Ah much better, my code-ocd has calmed itself now. \$\endgroup\$ Apr 13, 2015 at 18:00
  • 1
    \$\begingroup\$ About the idea: I think is really hard generat a lock from javascript, specially for client side, is very easy to skip. You should block with a proxy + some crazy script who block this type of content. You shuld ask in super user or something like that. about the test your code, you can "simulate" the time. with a new Date custom, configure outside of the range. \$\endgroup\$
    – vmariano
    Apr 13, 2015 at 18:28

3 Answers 3

2
\$\begingroup\$

I will comment on this part...

if (hours > 8 & hours < 15) {
    $("video").hide();
    $("video").parent().html("<b>Can you do us all a favor and not eat up all the bandwidth with your video watching?</b>" + "<br/>" + "<b>Thank you!</b>" + "<br/>" + "<br/>");
    $("embed").hide();
    $("embed").parent().html("<b>This is no time for childish games.</b>" + "<br/>" + "<b>Shouldn't you be studying or doing something related to school?</b>" + "<br/>" + "<br/>");
} else if(hours > 15 & hours < 8) {
    $("video").show();
    $("embed").show();
}

As Hosch said, you should be using the conditional operator &&.

Also, the else if clause wouldn't work because (hours > 15 && hours < 8) is only true if hours > 15 AND hours < 8, so it's never true. What you want is the conditional "or" operator: if (hours > 15 || hours < 8). But you don't even need to specify that if what you want is just to cover all the other cases: "if it's between 8 AM and 3:15 PM do this, otherwise do that".

So...

else { ... }

looks better.

Finally, hours > 8 && hours < 15 would only be true between 9 AM and 2 PM (instead of between 8 AM and 3 PM) as you're using the "greater than" (>) and "lesser than" (<) operators instead of "greater or equal" (>=) and "lesser or equal" (<=).

In order to cover the whole range you specified, you should write something like:

if ((hours >= 8 && hours < 15) || (hours == 15 && minutes <= 15)) { ... }
\$\endgroup\$
0
1
\$\begingroup\$

First of all, this looks like dead code:

if (minutes < 10) {
    var minutes = "0" + minutes
} else {}

The if doesn't do anything except assign a variable that immediately goes out of scope, and the else doesn't do anything at all.


Right here, I'm not sure why you use the logical & when you don't need it. You should be using the conditional && operator instead:

if (hours > 8 & hours < 15) {
    $("video").hide();
    $("video").parent().html("<b>Can you do us all a favor and not eat up all the bandwidth with your video watching?</b>" + "<br/>" + "<b>Thank you!</b>" + "<br/>" + "<br/>");
    $("embed").hide();
    $("embed").parent().html("<b>This is no time for childish games.</b>" + "<br/>" + "<b>Shouldn't you be studying or doing something related to school?</b>" + "<br/>" + "<br/>");
} else if(hours > 15 & hours < 8) {
    $("video").show();
    $("embed").show();
};

As for working, it appears that it will work - until a student figures out what is happening and turns the script off. I am not a JS expert, though, and I do not have the full context of how this will be run, so you should get another opinion. Personally, I would set it up and test it thoroughly over a day or so before deploying it.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the help. I've removed the else {}, changed the &s to && and I'm only using the var minutes = "0" + minutes; becuase I want a 0 to be added in front of 1-9 so it looks better. I don't know any other way to do it and it seems to be working, but if you have another way to do it feel free to tell me. I will be creating Google Chrome, Safari, and Firefox extensions that are managed. \$\endgroup\$
    – thrifus
    Apr 13, 2015 at 18:35
1
\$\begingroup\$

In addition to the errors pointed out above, your recursive call of setInterval() in timer() is going to set off \$ 2^t \$ new intervals every second so that by time \$t\$ you have a total number of intervals running of $$ 1+ \sum\limits_{n = 0}^t 2^n = 2^{t+1}$$ This may lead to some memory management issues overtime and cause the browser to crash.

If you still choose to go with the .js route, I propose a rewrite:

var poll = setInterval(timer, 1000);

function timer () {
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var minuteString = minutes < 10? "0" + minutes: minutes;

    $(".time").html(hours + ":" + minuteString);

    var now = new Date(2000, 01, 01, hours, minutes, 0);
    var start = new Date(2000, 01, 01, 8, 0, 0);
    var end = new Date(2000, 01, 01, 15, 0, 0);
    var disable = start <= now  && now <= end;

    if (disable) {
        // Disable video
    } else {
        // Enable video
    }

    //return disable;
}
\$\endgroup\$
1
  • \$\begingroup\$ I have no idea what that formula is, but thanks for your help. It seems like it loads faster now. \$\endgroup\$
    – thrifus
    Apr 15, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.