普通网友 2020-04-08 08:59
浏览 260

setTimeout的替代方法?

In the code below I am getting data from the response parameter and then I'm rendering a graph. Then I am exporting by getting a base64 string for the image from the imgData parameter and then I am setting the base64 string in the session using an AJAX call.

After that I'm making another AJAX call to create a report. In this AJAX call I make use of the image stored in the session as a base64 string. While printing the report I am getting a partial image. This is due to a large part of the data in the setTimeout function not working as expected in some cases.

When I remove the setTimeout function the image is not generating in the report because it makes another AJAX call before it completes the task.

Can anyone provide me a solution? Thanks in advance.

function LoadChart(response, callback) {
  if (response.length > 1) {
    //var aData = JSON.parse(response);
    document.getElementById('loading').style.display = "block";

    var aData = $.parseJSON(response);
    var chartData = [];
    $.each(aData, function(inx, val) {
      var obj = {};
      obj.AVARating = val.AVARating;
      obj.Per = val.Per;
      //obj.color = "#696969";
      chartData.push(obj);
    });

    var assessmentId = $('#hdnAssessmentId').val();
    var siteName = $('#hdnSiteName').val();
    var companyName = $('#hdnCompanyDesc').val();
    var countryName = $('#hdnCountryDesc').val();
    var latitude = $('#hdnLatitude').val();
    var longitude = $('#hdnLongitude').val();
    var division = $('#hdnDivisionDesc').val();
    var reportedDate = $('#hdnReportedDate').val();
    var username = $('#hdnWindowsID').val();
    var countryCode = $('#hdnCountryCode').val();
    var CompanyCode = $('#hdnCompanyCode').val();
    var SiteCode = $('#hdnSiteCode').val();
    var DivisionCode = $('#hdnDivisionCode').val();
    //am4core.disposeAllCharts();
    var chart = am4core.create("donutchart", am4charts.RadarChart);
    //chart.dataSource.url =   'VATReportGeneration.aspx?key=1&windowsId=' + username + '&divisionCode=' + DivisionCode + '&countryCode=' + countryCode + '&companyCode=' + CompanyCode + '&siteCode=' + SiteCode + '&assessmentId=' + assessmentId;
    chart.data = chartData;
    //chart.data = response;

    /* Add data */
    //chart.data = [{
    //    "AVARating": "Perimeter",
    //    "Per": 10
    //}, {
    //    "AVARating": "CCTV",
    //    "Per": 12
    //}, {
    //    "AVARating": "Intrusion Detection",
    //    "Per": 30
    //}, {
    //    "AVARating": "Information Protection",
    //    "Per": 40
    //}, {
    //    "AVARating": "Guarded forces",
    //    "Per": 50
    //}, {
    //    "AVARating": "Safe Haven",
    //    "Per": 60
    //}, {
    //    "AVARating": "Control Room",
    //    "Per": 70
    //}, {
    //    "AVARating": "Security Plan",
    //    "Per": 80
    //}, {
    //    "AVARating": "Procedures",
    //    "Per": 90
    //}];

    /* Create axes */
    var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
    categoryAxis.dataFields.category = "AVARating";

    var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
    valueAxis.renderer.axisFills.template.fill = chart.colors.getIndex(2);
    valueAxis.renderer.axisFills.template.fillOpacity = 0.05;
    valueAxis.renderer.gridType = "polygons";
    valueAxis.min = 0;
    valueAxis.max = 100;
    valueAxis.renderer.minGridDistance = 10;

    /* Create and configure series */
    var series = chart.series.push(new am4charts.RadarSeries());
    series.dataFields.valueY = "Per";
    series.dataFields.categoryX = "AVARating";
    series.strokeWidth = 3;
    series.fillOpacity = 0.3;

    var circleBullet = series.bullets.push(new am4charts.CircleBullet());
    circleBullet.circle.stroke = am4core.color("#fff");
    circleBullet.circle.strokeWidth = 2;

    setTimeout(function() {
      chart.exporting.getImage("png").then(function(imgData) {
        //console.log(imgData); // contains exported image data
        console.log(imgData);

        var image = imgData.replace('data:image/png;base64,', '');

        $.ajax({
          type: "POST",
          contentType: "application/json; charset=utf-8",
          url: "VATReportGeneration.aspx/SetImageInSession",
          data: JSON.stringify({
            "image": image
          }),
          dataType: "json",
          success: function(data) {
            if (data != '') {
              // document.getElementById('loading').style.display = "none";
              var url = "../../ReportHandler/VATConfigurationFileHandler.aspx";
              var file = url + '?VAT_Report=VAT_Report&assessmentId=' + assessmentId + '&siteName=' + siteName + '&companyName=' + companyName + '&countryName=' + countryName + '&latitude=' + latitude + '&longitude=' + longitude + '&division=' + division + '&reportedDate=' + reportedDate; //+'&image='+txt;
              $get("exporthandler").src = file;
              chart.dispose();
              document.getElementById('loading').style.display = "none";
            }
          },
          error: function(data) { }
        });
      });
    }, 5000);
  }
}
  • 写回答

1条回答 默认 最新

  • weixin_33686714 2020-04-08 09:05
    关注

    You can use setInterval

     setInterval(function() {
       // Code Here
     }, 3000);
    

    the 3000ms is the timeout of the interval

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀