title: Adding a pause in JS while file downloads
body: <p>I have a function as follows:</p>
  
  <pre><code> var audit ={
      init: function(){
        var dte = new Date();
        // hook into the get Accruals Link
        var accruals = $("#accruals");
        for (var i = 0, ii = accruals.length; i &lt; ii; i++){
          $(accruals).bind("click", audit.accrualsClicked);
        }
  
        // hook into the get closed routes button
        var glbtn = $(".glBtn");
        for (var i = 0, ii = glbtn.length; i &lt; ii; i++){
          $(glbtn).bind("click", audit.clickListener);
        }
  
        // hook into the GL Date1 element
        var glDate1 = $("#glDate1");
        for(var i = 0, ii = glDate1.length; i &lt; ii; i++){
          var myDate = null;
          if ($("#glDate1").attr('rel') != null) {
            myDate = $.datepicker.parseDate("yy-mm-dd", $("#glDate1").attr('rel'));
          }
          else {
            myDate = dte.getFullYear() + '-' + dte.getMonth()+ '-' + dte.getDate();
          }
          $('#glDate1').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: -120,
            maxDate: '+1D',//'+1M +10D',
            showAnim: 'fadeIn',
            altField: '#actualDate',
            altFormat: 'yy-mm-dd',
            changeMonth: true,
            numberOfMonths: 2,
            showButtonPanel: true,
            defaultDate: myDate
            //onSelect: audit.doInvDate
          });
          $(glDate1).val(myDate);
        }
        // hook into the GL Date2 element
        var glDate2 = $("#glDate2");
        for(var i = 0, ii = glDate2.length; i &lt; ii; i++){
          var myDate2 = null;
          if ($("#glDate2").attr('rel') != null) {
            myDate2 = $.datepicker.parseDate("yy-mm-dd", $("#glDate2").attr('rel'));
          }
          else {
            myDate2 = dte.getFullYear() + '-' + (dte.getMonth() +1) + '-' + dte.getDate();
          }
          $('#glDate2').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: -120,
            maxDate: '+1D',//'+1M +10D',
            showAnim: 'fadeIn',
            altField: '#actualDate',
            altFormat: 'yy-mm-dd',
            changeMonth: true,
            numberOfMonths: 2,
            showButtonPanel: true,
            defaultDate: myDate2
          });
          $(glDate2).val(myDate2);
        }
    },
    clickListener: function(event){
      audit.download(7);
    },
  
    accrualsClicked: function(Event){
      audit.download(6);
    },
    removeFile: function(fileName){
      var url     =  'http://'+window.location.hostname+'/truck/admin/export/service/removeFile.svc.php';
      var args    = "filename="+fileName;
      var res     =  audit.doAjax(url,args);
      //document.getElementById("Msg1").style.display = "none";
      if(res == '0'){
        alert('Failed to purge document: '+ fileName +' from file system')
      }
    },
    download: function(process){
      var gl1 = $('#glDate1').val();
      var gl2 = $('#glDate2').val();
      var url   =  'http://'+window.location.hostname+'/truck/admin/export/service/getFile.svc.php';
      var args  = "process="+process+"&amp;gl1="+gl1+"&amp;gl2="+gl2;
      var fileName =  audit.doAjax(url,args);
      if(fileName.length&gt;3){
        var fileurl = "http://"+window.location.hostname+"/truck/admin/export/service/" + fileName;
        window.location = fileurl;
        audit.removeFile(fileName);
      }
    },
    doAjax: function(url, args){
      var retVal;
      retVal =   $.ajax({  
                      type: "GET",
                      url: url,
                      data: args,
                      async: false
                  }).responseText;
      if(retVal==null || retVal=="")retval=99;
      return retVal;
    }
  }
  audit.init();
  </code></pre>
  
  <p>This works fine in creating the file and creating the download box. One requirement I have is to remove the file after it is created and the user downloads it. Is there a way to put some type of pause, or listener, so that the audit.removeFile(fileName) does not run until the user either downloads or cancels the request (reference the download: function(process) sub function)?</p>