【问题标题】:display a spinner during data processing在数据处理期间显示微调器
【发布时间】:2021-09-29 19:13:24
【问题描述】:

我为一些数据处理编写了一个 django 应用程序。在某些情况下,可能需要几分钟才能等待处理完成。因此,我想在用户单击“执行”后立即在页面上显示一个微调器,并在整个处理过程中,直到页面刷新以显示结果... 我遵循了一个教程 (here),我可以在其中设置应用程序并在页面加载时显示微调器。但这并不是我想要的。

以下 JS 代码来自视频并显示微调器,直到页面加载:

const spinnerbox = document.getElementById('spinner-box')
//console.log(spinnerbox)

$.ajax({
    type: 'GET',
    url: '/',
    success : function(response){
        spinnerbox.classList.add('not-visible')
        console.log('response', response)
    },
    error : function(error){
        console.log(error)
    }
})

我想我需要修改这段代码,类型应该是“POST”,但我不知道要在脚本旁边添加什么。 感谢您的帮助!

[编辑] 我可以使用这段代码更进一步:

const spinnerbox = document.getElementById('spinner-box') const submitbox = document.getElementById('submit-box')

submitbox.addEventListener('mouseup', ()=>{ spinnerbox.classList.remove('不可见')

  $.ajax({
      type: 'POST',
      url: '/',
      success : function(response){
          spinnerbox.classList.add('not-visible')
          console.log('response is success')
      },
      error : function(error){
          console.log('error')
          console.log(error)
          //spinnerbox.classList.add('not-visible')
      },
      // shows the loader element before sending.
      beforeSend: function() {
          console.log('In beforeSend...')
          //show spinner
          spinnerbox.classList.remove('not-visible')
      },
      // hides the loader after completion of request, whether successfull or failor.             
      complete: function() {
          console.log('In complete...')
          //spinnerbox.classList.add('not-visible')
      },
  })

})

但我猜这不干净。当我使用浏览器进行检查时,会发生以下情况:

In beforeSend... main.js:24:21
XHRPOSThttp://127.0.0.1:8000/
error main.js:18:21
Object { readyState: 0, getResponseHeader: getResponseHeader(e), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(e, t), overrideMimeType: overrideMimeType(e), statusCode: statusCode(e), abort: abort(e), state: state(), always: always(), catch: catch(e)
, … }
​
abort: function abort(e)​
always: function always()​
catch: function catch(e)​
done: function add()​
fail: function add()​
getAllResponseHeaders: function getAllResponseHeaders()​
getResponseHeader: function getResponseHeader(e)​
overrideMimeType: function overrideMimeType(e)​
pipe: function pipe()​
progress: function add()​
promise: function promise(e)
​
readyState: 0
​
setRequestHeader: function setRequestHeader(e, t)​
state: function state()
​
status: 0
​
statusCode: function statusCode(e)
​
statusText: "error"
​
then: function then(t, n, r)​
<prototype>: Object { … }
main.js:19:21
In complete...

塞巴斯蒂安

【问题讨论】:

    标签: javascript jquery django ajax


    【解决方案1】:

    您可以使用,ajax 允许您使用 beforeSend 添加一个操作,并且无论成功还是失败,ajax 完成时都会运行完成。

    const spinnerbox = document.getElementById('spinner-box')
    $.ajax({
        type: 'GET',
        url: '/',
        success : function(response){
            spinnerbox.classList.add('not-visible')
            console.log('response', response)
        },
        error : function(error){
            console.log(error)
        },
      // shows the loader element before sending.
      beforeSend: function() {
        //show spinner
     spinnerbox.classList.add('visible')
      },
      // hides the loader after completion of request, whether successfull or failor.             
      complete: function() {
        spinnerbox.classList.add('not-visible')
      },
    });
    

    【讨论】:

    • 您好,我使用了您的代码。刚刚修改了 beforeSend 函数:` beforeSend: function() { //show spinner spinnerbox.classList.remove('not-visible'). ` 在处理过程中,微调器根本不可见..
    • 我在代码中添加了一些痕迹:``` 在 beforeSend... main.js:18:17 XHRGEThttp://127.0.0.1:8000/ [HTTP/1.1 200 OK 18ms] 响应是成功 main.js:10:17 完成... main.js:24:17 ```
    • 您想要达到的终点是什么?让我试一试
    【解决方案2】:

    最简单的方法是: 使用不可见类创建一个微调器元素。 在 API 调用之前,将微调器设置为可见(删除不可见的类)。 API调用后,无论成功还是错误,重新设置为不可见。

    【讨论】:

    • 嗨@Sebastian,谢谢。你能描述一下你的意思吗(或者你会如何做“在 API 调用之前和 API 调用之后”
    【解决方案3】:

    您的服务器需要返回一个 json 以在同一页面中显示数据,或者您可以重定向到另一个 url。

      function sendViaAjax(){
    
        const spinnerbox = document.getElementById('spinner-box') // Get spinner
    
        spinnerbox.classList.add('visible') // turn spiner visible 
        $.ajax({
          type: 'POST', 
          url: '/url ', // you send data
          dataType: 'json',
          success : function(response){
            spinnerbox.classList.add('not-visible')
    
            console.log('response', response)
            window.location = "/url" // url you show data
            
          },
          error : function(error){
            console.log(error)
          }
       })
    
     }
    
    

    【讨论】:

      【解决方案4】:

      忘记视频中那个人做了什么关于设置课程not-visible。只需将 div 上的内联样式 设置为display:none

      微调器 div:

      <div class="text-center">
          <div class="spinner-border" role="status" style="display:none">
              <span class="visually-hidden">Loading...</span>
          </div>
      </div>
      

      现在在您的 js 中,您甚至可以使用淡入淡出效果,因为您也使用 jQuery 标记了您的问题。

      在你的 js 调用中:

      const spinnerbox = $(".spinner-border");
      
      // Fade in effect to start displaying the spinner div as the process starts...
      spinnerbox.fadeIn(1000); 
      
      $.ajax({
          type: 'POST',
          url: '/',
          data: whatever data to be processed here,
      
          success : function(response){
              // Fade out effect to stop displaying the spinner div as the process ends...
              spinnerbox.fadeOut(1000); 
              console.log('response', response);
          },
      
          error : function(error){
              // Fade out effect to stop displaying the spinner div as the process ends...
              spinnerbox.fadeOut(1000);
              console.log(error);
          }
      });
      

      【讨论】:

      • 你好@Moi Myazz,数据不是使用JS处理的,而是在python视图中处理的(这是一个django应用程序)。
      猜你喜欢
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 2019-12-12
      • 2013-02-08
      相关资源
      最近更新 更多