Advertisement
Guest User

Untitled

a guest
May 5th, 2016
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function start_long_task() {
  2.     // create a progress bar
  3.     var nanobar = new Nanobar({
  4.         bg: '#88f'
  5.     });
  6.  
  7.     // send ajax POST request to start background job
  8.     $.ajax({
  9.         type: 'POST',
  10.         url: '/longtask',
  11.         success: function(data, status, request) {
  12.             status_url = request.getResponseHeader('Location');
  13.             update_progress(status_url, nanobar);
  14.         },
  15.         error: function() {
  16.             alert('Unexpected dumb error');
  17.         }
  18.     });
  19. }
  20. function update_progress(status_url, nanobar) {
  21.     // send GET request to status URL
  22.     $.getJSON(status_url, function(data) {
  23.         // update UI
  24.         percent = parseInt(data['current'] * 100 / data['total']);
  25.         nanobar.go(percent);
  26.         if (data['state'] == 'PENDING' || data['state'] == 'PROGRESS') {
  27.             // rerun in 2 seconds
  28.             setTimeout(function() {
  29.                 update_progress(status_url, nanobar);
  30.             }, 100);
  31.         }
  32.     });
  33. }
  34. $(function() {
  35.     $('#start-bg-job').click(start_long_task);
  36. });
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement