Code set to operate at a given time

Asked

Viewed 65 times

1

I have a code that should work at a certain time, but it is not working.

$(function() {
    $(".post").each(function() {
        var d = new Date();
        var hora = d.getHours();
        if (hora <= 7 && hora >= 22) {
            alert("O código está funcionando!");
            if (_userdata.user_level == 2 || _userdata.user_level == 1) {
                return;
            }
            if ($('.pathname-box:contains(Tarefas), .pathname-box:contains(Redações)').length > 0) {
                var username = _userdata.username;
                if ($(this).has('.staff').length) {
                    return;
                }
                if ($(this).html().indexOf(username) <= 1) {
                    $(this).remove();
                }
                if ($(this).html().indexOf(username) >= 1) {
                    $(this).remove();
                }
            }
        }
    });
});

Part of setting the time:

            var d = new Date();
            var hora = d.getHours();
            if (hora <= 7 && hora >= 22) {

Observing: If I remove the part where you set the time when the code should work, it starts working so it can’t be code problem.

Code without schedule:

$(function() {
    $(".post").each(function() {
            alert("O código está funcionando!");
            if (_userdata.user_level == 2 || _userdata.user_level == 1) {
                return;
            }
            if ($('.pathname-box:contains(Tarefas), .pathname-box:contains(Redações)').length > 0) {
                var username = _userdata.username;
                if ($(this).has('.staff').length) {
                    return;
                }
                if ($(this).html().indexOf(username) <= 1) {
                    $(this).remove();
                }
                if ($(this).html().indexOf(username) >= 1) {
                    $(this).remove();
                }
            }
        }
    });
});
  • That condition if (hora <= 7 && hora >= 22) { is impossible. What is the number that is at once less than or equal to seven and greater than or equal to 22? Do you want to use the or? ie: ||

  • 1

    That’s what it was! Thanks.

1 answer

3


That condition if (hora <= 7 && hora >= 22) { is impossible. There is no number that is at once less than or equal to seven and greater than or equal to 22?

I think you want to use the operator or, that is to say: ||

if (hora <= 7 || hora >= 22) {

And so run the code between zero hours and seven (inclusive) and later after ten o'clock at night until the end of the day.

  • Even though it still doesn’t work, I even put an Alert() to see if something came up, but nothing.

  • 1

    Forget it... it did work! I had put ||| kkkkk. Many thanks friend!

  • Great! I’m glad I could help.

  • If you want to run between 7hrs and 22hrs the condition you need is: if (hour >= 7 && hour <= 22), I saw that in your code you did not follow the answer: http://answall.com/questions/92564/c%C3%B3digo-a-run-a-determined-hour

  • As you can see, it’s like it was before. But it wasn’t working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.