试图抵消异步代码执行错误

时间:2016-08-05 19:16:18

标签: javascript

我正在这里创建这个代码,它在过去的30天内获取每个日期并将每个日期推送到数组。然后在for each函数中,我查看每个日期并找到第二个日期(这是午夜之前的最后一分钟)。

然后我将代码运行到我的数据库,date是一个对象数组,介于我给它的两个日期之间。这部分有用......

这个代码异步执行有问题。刷新页面时会产生不同的结果。

            var today = new Date();
            var dates = [];
            var datesSimple = [];
            var counts= [];
            //console.log("today " + today)
            var year = today.getFullYear();
            var month = today.getMonth();
            var date = today.getDate();
            //console.log(dates);
            for(var i=30; i>0; --i){
                var day=new Date(year, month, date - i);
                //console.log(day);

                dates.push(day);
                datesSimple.push((day.getMonth() + 1) + '/' + day.getDate() + '/' +  day.getFullYear());
                if(i === 1){
                   var d = new Date();
            d.setHours(0,0,0,0);
            dates.push(d);
            //console.log(dates.length);

            datesSimple.push((day.getMonth() + 1) + '/' + (day.getDate()+1) + '/' +  day.getFullYear());
            var counter = 0;
            dates.forEach(function(date) {
                var tempDate = new Date(date);
                tempDate.setHours(23,59,59);

                var MatchmadeApps = Parse.Object.extend("matchMadeApps");
                var query = new Parse.Query(MatchmadeApps);
                query.greaterThan("createdAt", date);
                query.lessThan("createdAt", tempDate);
                query.limit(1000);
                query.find({
                    success: function(data) {
                        counts.push(data.length);
                        counter++;

                        if(counter === dates.length){
                            for(var i = 0; i<dates.length; i++){

                            }
                            console.log(counts);
                            console.log(dates);
                            var canvas = document.getElementById('updating-chart'),
                                                    ctx = canvas.getContext('2d'),
                                                    startingData = {
                                                    //x-axis data
                                                      labels: datesSimple,
                                                    //y-axis data
                                                      datasets: [
                                                         /* {
                                                              //data set 1
                                                              fillColor: "rgba(220,220,220,0.2)",
                                                              strokeColor: "rgba(220,220,220,1)",
                                                              pointColor: "rgba(220,220,220,1)",
                                                              pointStrokeColor: "#fff",
                                                              data: [65, 59, 80, 81, 56, 55, 40]
                                                          },*/
                                                          {
                                                              //data set 2
                                                              fillColor: "rgba(45, 158, 126,0.2)",
                                                              strokeColor: "rgba(151,187,205,1)",
                                                              pointColor: "rgba(151,187,205,1)",
                                                              pointStrokeColor: "#fff",
                                                              data: counts
                                                          }
                                                      ]
                                                    },
                                                    latestLabel = startingData.labels[6];

                                                // Reduce the animation steps for demo clarity.
                                                var myLiveChart = new Chart(ctx).Line(startingData, {animationSteps: 15});


                                                setInterval(function(){
                                                  // Add two random numbers for each dataset
                                                  //  myLiveChart.addData([Math.random() * 100, Math.random() * 100], ++latestLabel);
                                                    // myLiveChart.addData([Math.random() * 100], ++latestLabel);
                                                  // Remove the first point so we dont just add values forever
                                                    //myLiveChart.removeData();
                                                    //300000 ms is 5 minutes
                                                }, 1800000);
                            //console.log(counts);
                            //console.log(dates);
                        }
                    }
                });

            }); 
                }
            }

0 个答案:

没有答案