Ajax请求并非始终

时间:2019-02-13 08:17:34

标签: javascript ajax math

我一直在开发一个网站,该网站将提供人们创建的时间表。我正在使用Alienware进行开发,而ajax代码始终可以在笔记本电脑上完美运行。我正在使用IE。但是,如果我通过Samsung Smart TV或HTC U11手机访问站点,则Ajax不一致。它显示了我要的数据,但是,当我汇总一周的总时数时,有时会显示正确的小时数,有时会为用户安排一周的时数,显示0小时。为什么设备之间不一致?我知道Ajax建议用于IE。就像我说的那样,有时它会显示正确的每周总时数,而有时它只会显示零。我的Alienware上没有发生此问题。

我已经尝试使用onload代替onreadystatechange。我已将请求设置为异步和同步。无论在其他设备上将其设置为true还是false,都无法保持一致。我已经尝试过将其作为post和get方法,但两者也都不一致。我的代码有什么问题?

function getThisWeek(str5) {

    lastDate = new Date(str5);

    // getting dates for this week to be displayed...

    this.math = dayInput.length - 1;

    this.math0 = 0 - new Date(str5).getDay();

    // getting week total hour length, to determine how many times this code executes...
    weekSum = 0;
    totals = [];
    for (this.i = 0; this.i < weekTotalHours.length; this.i++) {

        str3 = this.i;
        this.date = new Date(str5);
        for (this.ii = (this.i * (7)) - new Date().getDay(); this.ii < (7 * (this.i + 1)) - new Date().getDay(); this.ii++) {

            totalHours = 0;

            this.date = new Date(str5);
            this.date = new Date(this.date.setDate(this.date.getDate() + (this.ii)));

            // which element position the total daily hours belongs to when displaying them...
            this.pos = this.ii + new Date().getDay();
            thisDayChart[this.pos].innerHTML = "";

                // displaying the date
                dayInput[this.pos].innerHTML = this.date.getDate();

                // highlighting today's date...
                if (this.date.getDate() == new Date(str5).getDate() && this.date.getMonth() == new Date().getMonth()) {
                dayInput[this.pos].style.color = "white";
                dayInput[this.pos].style.backgroundColor = "silver";
                dayInput[this.pos].style.borderRadius = "0px";
                thisDay[this.pos].style.border = "2px double lightblue";
                } else {
                dayInput[this.pos].style.color = "";
                dayInput[this.pos].style.backgroundColor = "";
                dayInput[this.pos].style.borderRadius = "";
                thisDay[this.pos].style.border = "";
                }

            // getting schedule from database...

            this.xmlhttp = new XMLHttpRequest();
            this.xmlhttp.pos = this.pos;
            this.xmlhttp.ipos = this.i;
            this.xmlhttp.d = months[this.date.getMonth()] + " " + this.date.getDate() + ", " + this.date.getFullYear();
            this.xmlhttp.onreadystatechange = function() {

                if (this.readyState == 4 && this.status == 200) {

                    //alert(this.responseText);

                    dayInput[this.pos].m = months[new Date(this.d).getMonth()];
                    dayInput[this.pos].weekDay = days[new Date(this.d).getDay()];

                    // how many weeks in the year is user...

                    this.islpyr = new Date(this.d);
                    this.islpyr = this.islpyr.setMonth(1);
                    this.islpyr = new Date(new Date(this.islpyr).setDate(29));

                    if (this.islpyr.getMonth() == 2) {
                    mlens[1] = 28;
                    } else {
                    mlens[1] = 29;
                    }

                    // getting total amount of days for the year...
                    this.yrlen = 0;
                    for (this.iii = 0; this.iii < mlens.length; this.iii++) {
                        this.yrlen += mlens[this.iii];
                    }

                    // getting total weeks
                    this.wklen = this.yrlen / 7;
                    this.wklen = Math.round(this.wklen);

                    this.week = 0;
                    for (this.iii = 0; this.iii < new Date(this.d).getMonth(); this.iii++) {

                        this.week += mlens[this.iii];

                    }
                    this.week += new Date(this.d).getDate();

                    this.week /= 7;
                    this.week = Math.round(this.week) + 1;
                    // rounding and displaying current week number...

                    if (new Date(this.d).getDay() == 0) {
                    myDayWrapper[this.ipos].weekLabel = "Week " + this.week + " of " + this.wklen;
                    }

                    if (this.pos == 0) {
                    weekDisplay[0].innerHTML = myDayWrapper[0].weekLabel;
                    }

                    // which day of the year it is... how many days in the year is the user...

                    this.yrpos = 0;
                    for (this.iv = 0; this.iv < new Date(this.d).getMonth(); this.iv++) {

                        this.yrpos += mlens[this.iv];

                    }

                    this.yrpos += new Date(this.d).getDate();

                    thisDayTitle[this.pos].innerHTML = "Day " + this.yrpos + " of " + this.yrlen;

                    totalHours = 0;

                    this.schedule = "" + this.responseText;

                    // if user is unscheduled, do this first... 'undefined' literally means there isn't a schedule for that specific day.
                    if (this.schedule.search("Undefined") >= 0) {

                        this.elem = document.createElement("div");
                        this.elem.setAttribute("class","not_scheduled");
                        this.elem.innerHTML = "Unscheduled";
                        thisDayChart[this.pos].appendChild(this.elem);

                        this.elem = document.createElement("div");
                        this.elem.setAttribute("class","pick_up_shift_btn");
                        this.elem.innerHTML = "Claim Shifts";
                        this.elem.i = this.pos;
                        this.elem.ipos = this.ipos;

                            this.elem.onclick = function() {

                                popUpWrap[1].style.display = "block";
                                this.datesd = [];
                                for (this.i = this.ipos * 7; this.i < 7 * (this.ipos + 1); this.i++) {
                                this.datesd.push(dayInput[this.i].innerHTML);
                                }

                                for (this.i = 0; this.i < 7; this.i++) {

                                shiftClaimDate[this.i].innerHTML = this.datesd[this.i];
                                popUpShiftDay[this.i].innerHTML = dayInput[this.i].weekDay;
                                this.strrr = "" + dayInput[this.i].m;
                                this.strrr = this.strrr.substring(0,3);
                                shiftClaimMonth[this.i].innerHTML = this.strrr;
                                }

                            }

                        thisDayChart[this.pos].appendChild(this.elem);

                        if (totals.length - 1 == this.pos) {
                        } else {
                        totals.push(0);
                        }

                    } else {

                        // getting daily total...

                        this.schedule = this.schedule.split(",");
                        for (this.iii = 0; this.iii < this.schedule.length; this.iii++) {

                            this.str = "" + this.schedule[this.iii];
                            this.str = this.str.split("=");

                            this.dateString = "December 22, 1991";
                            switch (this.str[0]) {

                                case "shift_start":

                                    this.start = new Date(this.dateString + " " + this.str[1]);
                                    this.end = this.schedule[this.schedule.length - 1] + "";
                                    this.end = this.end.split("=");
                                    this.end = new Date(this.dateString + " " + this.end[1]);

                                    this.hours = (this.end.getHours() - this.start.getHours()) * 60;
                                    this.hours = this.hours - (this.start.getMinutes() + this.end.getMinutes());
                                    this.hours /= 60;

                                    totalHours += this.hours;

                                    weekSum += totalHours;

                                break;

                                case "break_start":

                                    this.start = new Date(this.dateString + " " + this.str[1]);
                                    this.end = this.schedule[this.iii + 1] + "";
                                    this.end = this.end.split("=");
                                    this.end = new Date(this.dateString + " " + this.end[1]);

                                    this.hours = (this.end.getHours() - this.start.getHours()) * 60;
                                    this.hours = this.hours - (this.start.getMinutes() + this.end.getMinutes());
                                    this.hours /= 60;

                                    weekSum -= this.hours;
                                    totalHours -= this.hours;

                                    this.s = "s";
                                    if (totalHours == 1) {
                                    this.s = "";
                                    }


                                    thisDayChart[this.pos].innerHTML = totalHours + " hour" + this.s;

                                break;

                            }

                        }

                    }

                    weekTotalHours[this.ipos].innerHTML = "Week Total: " + weekSum;
                    //alert(weekSum);
                   // displaying hour totals...
                    if (new Date(this.d).getDay() == 6) {
                    weekSum = 0;
                    }
                    totalHours = 0;
                }


            }

            this.xmlhttp.open("GET","/js/data/schedule.html?date="+ months[this.date.getMonth()] + " " + this.date.getDate() + ", " + this.date.getFullYear(),false);
            this.xmlhttp.send();

        }

    }

}

我希望每台设备与应该在其上运行的每台设备上显示的小时总数保持一致。

0 个答案:

没有答案
相关问题