重置JavaScript中的函数或变量以清除ajax的先前结果

时间:2016-12-01 02:47:20

标签: javascript php jquery ajax

我之前有一个问题也发布在这里。出现了另一个问题。似乎我的ajax javascript的先前结果仍然存在,我需要不显示。我为变量尝试了delete,undefined和null,但它仍然显示了我之前查询的结果。

这是代码

请注意,有些括号可能会丢失,因为我只复制了我的实际代码的一部分。

$("#telcooptions").change(function () {
$('#telcofilter').val($("#telcooptions").val());
var telco  = $("#telcooptions").val();
var sdate  = $("#datepicker").val();
var edate  = $("#datepicker1").val();
var region = $("#region").val();
alert(region);
  $.ajax({
  url: "ajax/month.php",
  dataType: "json", 
  type: 'POST',
  data: { telcos: telco, start: sdate, end: edate, regions: region},

  success: function (dataofconfirm) {
    month = dataofconfirm;
    //-----------------------
    //-START OF PRICE AJAX
    //-----------------------
    $.ajax({
      url: "ajax/prices.php",
      dataType: "json",
      type: 'POST',
      data: { telcos: telco, start: sdate, end: edate, regions: region},

      success: function (dataofconfirm) {
        price = dataofconfirm;
        //-----------------------
        //- MONTHLY SALES CHART -
        //-----------------------

        months = month;
        prices = price;

        alert(months);
        alert(prices);
        // Get context with jQuery - using jQuery's .get() method.
        var salesChartCanvas = $("#salesChart").get(0).getContext("2d");
        // This will get the first returned node in the jQuery collection.
        var salesChart = new Chart(salesChartCanvas);

        var salesChartData = {
          labels: months,
          datasets: [
            {
              label: "XXXXX",
              fillColor: "rgb(255,40,40)",
              strokeColor: "rgb(255,40,40)",
              pointColor: "rgb(253,16,16)",
              pointStrokeColor: "#c1c7d1",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgb(220,220,220)",
              data: price
            }
          ]
        };

        var salesChartOptions = {
          //Boolean - If we should show the scale at all
          showScale: true,
          //Boolean - Whether grid lines are shown across the chart
          scaleShowGridLines: false,
          //String - Colour of the grid lines
          scaleGridLineColor: "rgba(0,0,0,.05)",
          //Number - Width of the grid lines
          scaleGridLineWidth: 1,
          //Boolean - Whether to show horizontal lines (except X axis)
          scaleShowHorizontalLines: true,
          //Boolean - Whether to show vertical lines (except Y axis)
          scaleShowVerticalLines: true,
          //Boolean - Whether the line is curved between points
          bezierCurve: true,
          //Number - Tension of the bezier curve between points
          bezierCurveTension: 0.3,
          //Boolean - Whether to show a dot for each point
          pointDot: true,
          //Number - Radius of each point dot in pixels
          pointDotRadius: 4,
          //Number - Pixel width of point dot stroke
          pointDotStrokeWidth: 1,
          //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
          pointHitDetectionRadius: 20,
          //Boolean - Whether to show a stroke for datasets
          datasetStroke: false,
          //Number - Pixel width of dataset stroke
          datasetStrokeWidth: 2,
          //Boolean - Whether to fill the dataset with a color
          datasetFill: true,
          //String - A legend template
          legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].lineColor%>\"></span><%=datasets[i].label%></li><%}%></ul>",
          //Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
          maintainAspectRatio: false,
          //Boolean - whether to make the chart responsive to window resizing
          responsive: true
        };

        //Create the line chart
        salesChart.Line(salesChartData, salesChartOptions);
        //---------------------------
        //- END XXXXXXCHART -
        //---------------------------
       }
    });
    //-----------------------
    //-END OF XXXXX AJAX
    //-----------------------
   }
});

});

如何重置变量甚至整个函数本身。

1 个答案:

答案 0 :(得分:0)

好的,我做到了。对不起麻烦的家伙们。我做的是这个

$('#salesChart').remove();
        $('.chart').append('<canvas id="salesChart" style="height: 180px;"></canvas>');

谢谢大家。发布这个也是为了帮助别人。