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

可能-如果不在x秒内加载,是否取消用户控件呈现?

  •  0
  • Steve  · 技术社区  · 16 年前

    这可能是一个云雀,但对于recaptcha控件,因为有时需要很长的时间来渲染,这是可能的吗?

    如果渲染时间超过5秒,我想停止渲染对象并显示我自己的验证码。

    我会在页面加载时启动一个计时器,如果已经过了5秒,在recaptcha控件(prerender?),我会取消渲染,或使其不可见,或使其具有这种效果。它是第三方用户控件,因此我没有源代码。

    更新:

    我在发布后尝试了下面的代码。如果用户控件无法连接其服务器,也就是说,我将断开与Internet的连接,但当控件等待服务器返回时,如果有很长的暂停时间,则无法感觉到这一点。即使我将毫秒间隔更改为1,控件也会呈现。

    <MTAThread()> _
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            Dim ucChk As New UCExistenceChecker(recaptcha, Me)
            Dim doFindUC As System.Threading.TimerCallback = AddressOf ucChk.FindUC
            Dim stateTimer As System.Threading.Timer = New System.Threading.Timer(doFindUC, Nothing, 0, 5000)
        End If
    
    End Sub
    
    Public Class UCExistenceChecker
    
        Dim _r As Recaptcha.RecaptchaControl
        Dim _pg As Page
    
        Sub New(ByVal r As Recaptcha.RecaptchaControl, ByVal pg As Page)
            _r = r
            _pg = pg
        End Sub
    
        Sub FindUC(ByVal stateInfo As Object)
            If _pg.FindControl("recaptcha") Is Nothing Then
                _r.SkipRecaptcha = True  'This "unrenders" the control, sort of.
            End If
        End Sub
    
    End Class
    
    1 回复  |  直到 13 年前
        1
  •  1
  •   Jonathan Leffler    13 年前

    您可以使用iframe来包含captcha块并订阅readyStateChanged或layoutComplete事件。然后,您可以使用setTimeout()来安排一些javascript在您希望等待的最长时间之后运行,如果这两个事件都没有触发,则从dom中删除iframe并将其替换为您自己的。