Advertisement
virbo

progresbar-ajax

Mar 4th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(".btn-proses").click(function() {
  2.     prosesOpen(true);
  3. });
  4.  
  5. function prosesOpen(event) {
  6.     $.ajax({
  7.         type: 'POST',
  8.         url: get.urlOpen,
  9.         data: {start: event},
  10.         beforeSend: function(){
  11.             disableButton(true);
  12.             $(".pesan").hide();
  13.         },
  14.         success: function(event){
  15.             getJson = $.parseJSON(event);
  16.  
  17.             if (getJson.persen >= 0) {
  18.                 bar(getJson.hasOpen, getJson.total, getJson.proses, getJson.persen);
  19.                 statusProses(getJson.countSuccess, getJson.countError, getJson.totalCount);
  20.                 if (getJson.persen < 100) {
  21.                     prosesOpen();
  22.                 } else {
  23.                     tampilPesan('Finish .. !!', getJson.message, false);
  24.                     disableButton(false);
  25.                 }
  26.             }
  27.         },
  28.         error: function (status, exception) {
  29.             getErrorMessage(status, exception);
  30.             disableButton(false);
  31.             bars(0);
  32.         }
  33.     });
  34. }
  35.  
  36. function disableButton(event) {
  37.     if (event === true) {
  38.         $('.btn-proses').attr('disabled', 'disabled');
  39.         $('.btn-kembali').attr('disabled', 'disabled');
  40.     } else {
  41.         $('.btn-proses').removeAttr('disabled');
  42.         $('.btn-kembali').removeAttr('disabled');
  43.     }
  44. }
  45.  
  46. function statusProses(success, error, total) {
  47.     $("#success").html(success);
  48.     $("#error").html(error);
  49.     $("#total").html(total);
  50. }
  51.  
  52. function bar(open, total, proses, persen) {
  53.     $(".progress-bar").css("width",persen+"%");
  54.     $(".progress-number").html("Proses "+open+" dari <strong>"+total+"</strong> ("+persen+"%)");
  55. }
  56.  
  57. function tampilPesan(title, msg, error){
  58.     if (error == true) {
  59.         $(".pesan").attr('class','alert alert-danger pesan');
  60.     } else {
  61.         $(".pesan").attr('class','alert alert-success pesan');
  62.     }
  63.     $(".pesan").show();
  64.     $("#title").html(title);
  65.     $("#isi_pesan").html(msg);
  66. }
  67.  
  68. function getErrorMessage(stat, exception) {
  69.     var msg = '';
  70.     var title = '';
  71.     if (stat.status === 0) {
  72.         msg = stat.responseText;
  73.         title = 'Error '+stat.status;
  74.     } else if (stat.status == 404) {
  75.         msg = stat.responseText;
  76.         title = 'Error '+stat.status;
  77.     } else if (stat.status == 500) {
  78.         msg = stat.responseText;
  79.         title = 'Error '+stat.status;
  80.     } else if (exception === 'parsererror') {
  81.         msg = stat.responseText;
  82.         title = 'Error '+stat.status;
  83.     } else if (exception === 'timeout') {
  84.         msg = stat.responseText;
  85.         title = 'Error '+stat.status;
  86.     } else if (exception === 'abort') {
  87.         msg = stat.responseText;
  88.         title = 'Error '+stat.status;
  89.     } else {
  90.         msg = 'Uncaught Error.\n' + stat.responseText;
  91.         title = 'Error '+stat.status;
  92.     }
  93.     tampilPesan(title, msg, true);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement