代码之家  ›  专栏  ›  技术社区  ›  Peanut

ASP.NETMVC-实时ajax请求

  •  1
  • Peanut  · 技术社区  · 14 年前

    我通过使用jQuery ajaxSend方法使对话框可见来实现这一点。我使用jQuery ajaxComplete方法关闭对话框。

    我肯定都是些很平常的事情。

    但是,如果调用所用的时间非常短(比如10毫秒),那么我希望等到对话框可见后的一秒钟之后再关闭对话框。原因是提供用户反馈。。。并停止可怕的闪烁的对话框快速打开,然后关闭。

    如何使用jQuery和/或ajax实现这一点?

    2 回复  |  直到 13 年前
        1
  •  1
  •   redsquare    14 年前

    注意我在这里使用beforesend和success来伪造代码

      var timerid;
    
      beforeSend: function(){
    
           //open the dialog in 100 milliseconds
           timerid = window.setTimeout( function(){
    
                $('#dialog').dialog('close');
    
           }, 100)
    
        },
      success: function(){
    
          //cancel the showing of the dialog if not already called
          window.clearTimeout( timerid );
          //close the dialog regardless
          $('#dialog').dialog('close');
      }
    

    如果您不喜欢这个想法,那么simplay将dialog close函数放在ajax完全回调中的setTimeout中

    complete : function(){
    
       //close the dialog in 1 second
       window.setTimeout( function(){
    
            $('#dialog').dialog('close');
    
       }, 1000)
    
    }
    
        2
  •  0
  •   Peanut    14 年前

    我已经为这个问题自己找到了一个解决方案——基于“红方”的巨大反响和一些进一步的阅读。

    我使用了redsqure中的代码,只在给定的持续时间过后才打开模态对话框,因此希望根本不必打开模态对话框。

    当模态打开时,我添加了代码,以确保它保持打开至少800毫秒。。。只是为了避免它在屏幕上快速闪烁的可能性。为了实现这一点,我在'ajaxSend'方法中启动一个javascript计时器,然后使用'ajaxComplete'方法来确定模式是否打开。如果是这样的话,我就用定时器来计算它被打开了多长时间,然后把时间差补到800毫秒。我把在网上找到的剧本改编成我的计时器。下面的脚本。

    var timer_ms = 0;
    var timer_state = 0;
    
    /// <summary>
    /// Responsible for starting / stopping the timer. Also calculates time.
    /// </summary>
    function timerStartStop() {
        if (timer_state == 0) {
            timer_ms = 0;
            timer_state = 1;
            then = new Date();
            then.setTime(then.getTime() - timer_ms);
        }
        else {
            timer_state = 0;
            now = new Date();
            timer_ms = now.getTime() - then.getTime();
        }
    }
    
    /// <summary>
    /// Resets the timer.
    /// </summary>
    function timerReset() {
        timer_state = 0;
        timer_ms = 0;
    }
    

    谢谢。