I make a calendar (principle - we are scrolling through slides for 7 days), so far I have done everything below, but the console suddenly gave me an error - Uncaught ReferenceError: dateup is not defined , the most important thing (!) - its reason is this block of code -

 $(".all-month .items-date:last-child").each(function () { if($(this).find(".cube:last").html()<10){ $(this).remove(); } }); 

if you remove it, the error will disappear, but how is it related to the fact that the variable just disappears, I can not understand.
Another thing I can not understand is that the toggle_month variable after the function does not change, although it is global (I checked it on a separate primitive function - the variable changes in a separate file, but in that where the calendar handler is not, that is, you , this problem simply can not be reproduced), there is at least a guess why this is happening?

 function add_month_time(a, month, nextprev) { $.ajax({ type: "POST", url: document.getElementById('calendar_component_path').value + "/ajax.php", data: {ajax_time: true, month: month, sessid: BX.bitrix_sessid()}, cache: false, dataType: 'html', beforeSend: function(){ renderLoader(a, month, nextprev); }, timeout: 8000, success: function (html) { // данные пришли корректно if(html){ removeLoader(); removeError(); if (nextprev == "next") {$(".gooajax").append(html);} if (nextprev == "prev") {$(".gooajax").prepend(html);} //костыль для удаления страницы без содержимого $(".all-month .items-date:last-child").each(function () { if ($(this).html() == "")$(this).remove(); }); //костыль для определения есть ли в видимой неделе начало следующей $(".all-month .items-date:last-child").each(function () { if($(this).find(".cube:last").html()<10){ $(this).remove(); } }); $(".all-month").each(function () { $(this).find(".items-date:last").addClass("end-week"); }); var week_today = $("#today").parent().parent(); if (a == 1) { week_today.show(); week_today.parent().show(); } ///вызывается 1 раз при формировании, если попали на крайнюю неделю - подгружаем следующую if (a) { //если мы на последней неделе сразу аяксим следующую if (week_today.hasClass("end-week")) { $(".control-items-date.next").html("Следующий месяц"); toggle_month = "next"; console.log(toggle_month); if (!(parseFloat($(".all-month:visible").data("month")) < parseFloat(dateup))) { dateup = parseFloat(dateup) + 1; if (dateup < 10)dateup = "0" + dateup; add_month_time(0, dateup, "next"); } } //если мы на первой неделе сразу аяксим предыдущую if (week_today.hasClass("start-week")) { $(".control-items-date.prev").html("Предыдущий месяц"); toggle_month = "prev"; if (!(parseFloat($(".all-month:visible").data("month")) > parseFloat(datedown))) { datedown = parseFloat(datedown) - 1; if (datedown < 10)datedown = "0" + datedown; add_month_time(0, datedown, "prev"); } } }//if(a) }else{ removeLoader(); renderError(a, month, nextprev); } }//success end });//ajax end }//function end BX.ready(function() { var dateup = $("#cur_date").val(); var datedown = $("#cur_date").val(); var bool = false; var toggle_month = 0; var slide_month; var current_month; var control_next = $(".control-items-date.next"); var control_prev = $(".control-items-date.prev"); add_month_time(1, dateup, "next"); ... 

and this is the DOM structure of the calendar:

  <div class="all-month" data-month="09" style="display: block;">//контейнер для месяца <div class="month fs22 regular">Сентябрь</div>//имя месяца <div class="items-date start-week">//слайд с 7 днями недели <div class="relative item fs14 regular">//сам день <div class="cube medium fs13">29</div> <div class="day regular fs14">Понедельник</div> <div class="c"></div> <span class="l fs22 regular gray_text">с 09:00 до 19:00</span> </div> ... 
  • one
    Change the toggle_month check after calling add_month_time inside BX.ready ? - Arnial September
  • Yes, in the onclick handler, this variable should have a value at which the slide shuffles, but it does not. starts working only after clicking back and forth (the necessary values ​​are set in the handler and everything starts working) - Maximmka

1 answer 1

as always, it was enough to ask a question - the solution was found by itself. the BX.ready(function() { that I declare variables inside the BX.ready(function() { keyword - functions, and everything inside is local, so the variables did not change when calling other functions that should change them, so everything worked in a separate file