Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.setInterval logic (VBS)

Status
Not open for further replies.

bslintx

Technical User
Apr 19, 2004
425
US
hey all...i have no idea why i can't get the logic down with this...

i have an hta...in which cannot use sleep so ended up choosing window.setInterval

the problem: i have successfully displayed the loader however...my logic was off...i was displaying the loader delay>loop>display loop results

i would have said screw it...however...with large returns...this is not feasible...so...

how do i simply show a loader(animated.gif) WHILE the loop is collecting data? ...it's rather embarrassing to say...but i simply cannot get it to work :eek:

thanks

Code:
<html>
 <head>
  <title>Student Account</title>
   <style>
    div.results {width:100%;height:300px;overflow-y:scroll;}
    table {border:1px solid black;width:75%;}
    th {width:33%;background:#eee;padding-left:75px;}
    table.results {border:none;width:100%;}
    td {text-align:center;}
    img.loader {position:relative;left:50px;top:25px;}
   </style>

   <hta:application 

    windowState="normal">

  <script language="VBScript">
    Dim oConn, iTimerID
    Sub DisplayStudentAccts(sUserInput)

     If Len(sUserInput) > 0 Then     
       
      Set oRootDSE = GetObject("LDAP://RootDSE")
      sConfig = oRootDSE.Get("configurationNamingContext") 
      sDomainAdsPAth = oRootDSE.Get("defaultNamingContext")
      sAdsPath = "OU=Student Users," & sDomainAdsPAth
      Set oConn = CreateObject("ADODB.Connection")
      oConn.Provider = "ADsDSOObject"
      oConn.Open "Active Directory Provider"

      on error resume next
 
      Set oRs = oConn.Execute("SELECT cn, adspath " & _
                              "FROM 'LDAP://" & sAdsPath & "'" & _
                              "WHERE cn='" & sUserInput & "*'")

      If Not oRs.EOF Then       
       
       sRowColor = "white"  
       
       iTimerID = window.setInterval("DisplayLoader", 500,"vbscript")
       Do While Not oRs.EOF
        
        iCount = iCount + 1
        iStuAcctTot = oRs.Recordcount
        sAdsPath = oRs("ADsPath")
        Set oStudentAcct = GetObject(sAdsPath)
        dPwdLastChanged = oStudentAcct.PasswordLastChanged

       If iCount < iStuAcctTot Then 

        DisplayLoader
        
        If Len(dPwdLastChanged) = 0 Then dPwdLastChanged = "Password never set"

        sResults = sResults & "<tr bgcolor=""" & sRowColor & """>" & _
			      " <td>" & iCount & "</td>" & _
                              " <td>" & oRs("cn") & "</td>" & _
                              " <td>" & dPwdLastChanged & "</td>" & _
                              " <td><input type=""checkbox"" name=""stuacctck"" value=""" & oRs("cn") & """>" & _
                              "</tr>"

        If sRowColor = "white" Then
         sRowColor = "#eeeeee"
        Else
         sRowColor = "white"
        End If

       Else
        window.clearInterval(iTimerID)
       End If
        
       oRs.MoveNext
       Loop       

       results.innerhtml = "<table cellspacing=""1"">" & _
                           " <tr>" & _
			   "  <th>#</th>" & _
                           "  <th>Student Account</th>" & _
			   "  <th>Password Last Changed</th>" & _
                           "  <th><a href=""#"" onclick=""checkAll(stuacctck)"">Select All</th>" & _
                           " </tr>" & _
                           " <tr>" & _
                           "  <td colspan=""4"">" & _
                           "   <div class=""results"">" & _
                           "    <table class=""results"">" & _
                                  sResults & _
                           "    </table>" & _
                           "   </div>" & _
                           "  </td>" & _
                           " </tr>" & _
                           "</table>"
      Else
       results.innerhtml = "<img src=""[URL unfurl="true"]http://webs.wichita.edu/depttools/depttoolsmemberfiles/accomp/question_mark%20(WinCE).jpg"">"[/URL]
      End If
     End If
    End Sub

    Sub DisplayLoader               
     results.innerHtml = "<img class=""loader"" src=""[URL unfurl="true"]http://www.dzone.com/images/std/ajax-loader-85.gif"">"[/URL]     
    End Sub
   </script>
 </head>
 <body>
  
 
  Student Account: <input id="stuacct">
                   <input type="button" value="Go" onclick="DisplayStudentAccts(stuacct.value)">
                
  <div id="results"></div>  
 
 </body>
</html>
 
You may not have realized this but vbscript and htas are single threaded. The way to achieve what you wish is to split the work using the timer.
Code:
    ' Set the timer to go off first
    setTimeout "Kick",500,"vbscript"
 
    Set oRs = oConn.Execute("SELECT cn, adspath " & _
                              "FROM 'LDAP://" & sAdsPath & "'" & _
                              "WHERE cn='" & sUserInput & "*'")
end sub

sub DisplayRec
    dim iCountLast
    ' You need to adjust this to how many it will do in 400ms
    ' Don't display the whole lot: only enough to stop your
    ' animation looking bad
    iCountLast = iCount + 10
       
       sRowColor = "white"  
       
       Do While iCount < iCountLast and Not oRs.EOF
        
          ...
    loop
end sub

Sub DisplayLoader               
     results.innerHtml = "<img class=""loader"" src=""[URL unfurl="true"]http://www.dzone.com/images/std/ajax-loader-85.gif"">"[/URL]     
End Sub

sub Kick
   DisplayLoader
   DisplayRec
   setTimeout "Kick",500,"vbscript"
end sub
 
xwb,

firstly, i realy appreciate the time with trying to help...

unfortunately, i still cannot grasp the delay logic

what i have done was break down into logical chunks...hopefully this will help with placing the delay logic...i also cut out any fat...only the bare minimum code to

1. input user data
2. query user data against active dir using ldap/sql
3. collect data from active dir
4. display sql results

as you already know...i am trying to display a placeholder animated gif to show user 'something' is going on while the data is being concatenated into html table rows and ultimately concatenating into a table to display to user

the mod has been tested and works fine...i placed the delay logic however and i wasn't able to show the .gif...also...is there a way to turn 'off' the delay logically? after the results are posted...my cpu is being pounded

i usually can figure out smaller things like this...i apologize in advance...since the code is written i'm not asking to write...just replace the logic if not too much to ask...you have know idea how much it would help..it's been driving me nuts...thanks!


note...i wasn't sure about the code you posted...the first sub for example was missing and i wasn't sure if needed to be placed in my 'main' or your 'kick'...please advise...thanks
Code:
<html>
 <head>
  <hta:application />

  <script language="VBScript">
   Dim oConn
  
   Function CollectStudentAccts 
    sUserInput = stuacct.value

    If Len(sUserInput) > 0 Then    
     OpenActDir
     Set oRs = oConn.Execute("SELECT cn " & _
                             "FROM 'LDAP://" & GetAdsPath & "'" & _
                             "WHERE cn='" & sUserInput & "*'")
     If Not oRs.EOF Then
      CollectStudentAccts = oRs.GetRows      
     Else
      results.innerhtml = "No Recs"
     End If 
    End If
   End Function    
   
   Sub DisplayStuAccts
    Dim iCountLast
    
    If isArray(CollectStudentAccts) Then
     For each stu_acct in CollectStudentAccts
      sResults = sResults & "<tr>" & _
    	  	            " <td>" & stu_acct & "</td>" & _
                            "</tr>"
     Next
     results.innerhtml = "<table border=""1"">" & sResults & "</table>"
    End if
   End Sub

   Sub DisplayLoader               
    results.innerHtml = "<img class=""loader"" src=""[URL unfurl="true"]http://www.dzone.com/images/std/ajax-loader-85.gif"">"[/URL]     
   End Sub

   Sub Main
    CollectStudentAccts
    DisplayLoader
    DisplayStuAccts
    setTimeout "Main",500,"vbscript"
   End Sub
   
   Function GetAdsPath
    Set oRootDSE = GetObject("LDAP://RootDSE")
    sRootAdsPath = oRootDSE.Get("defaultNamingContext")
    GetAdsPath = "CN=Users," & sRootAdsPath
   End Function

   Function OpenActDir
    Set oConn = CreateObject("ADODB.Connection")
    oConn.Provider = "ADsDSOObject"
    oConn.Open "Active Directory Provider"
   End Function       
  </script>
 </head>
 <body> 
  Student Account: <input id="stuacct">
                   <input type="button" value="Go" onclick="Main">
                   <div id="ct"></div>

  <div id="results"></div> 
 </body>
</html>
 
ok...i went with my original logic...i was on the right track...however, what threw me off was NOT dimming my vars...so to those out there that want to learn a valuable lesson...use

OPTION EXPLICIT :)

anyhoot here is the bare-boned version...may indeed come in handy for an ad admin or a database admin....codeshould be easy to modify to suit ones needs....

it's extremely accurate in delay...i have tested and "think" i got the bugs...thanks again xwb for trying to help!!!!

Code:
<html>
 <head>
  <hta:application />

  <script language="VBScript">
   Option Explicit 

   Dim oConn, oRs
   Dim oRootDSE, sRootAdsPath
   Dim i, iTimerID, iStuAcctTot, aStuAccts, sResults, sUserInput 
    
   Function CollectStudentAccts
    sUserInput = stuacct.value
       
    If Len(sUserInput) > 0 Then
     OpenActDir
     Set oRs = oConn.Execute("SELECT cn " & _
                             "FROM 'LDAP://" & GetAdsPath & "'" & _
                             "WHERE cn='" & sUserInput & "*'")
     If Not oRs.EOF Then      
      CollectStudentAccts = oRs.GetRows   
     End If 
    End If
   End Function    
   
   Sub DisplayStuAccts      
    If i <= iStuAcctTot Then       
     If i = i mod 2 Then DisplayLoader
      sResults = sResults & "<tr>" & _
     	                    " <td>" & aStuAccts(0,i) & "</td>" & _
                            "</tr>"
     i = i + 1
     
    Else                 
     window.clearInterval(iTimerID)          
     results.innerhtml = "<table border=""1"">" & sResults & "</table>"
     sResults = ""     
    End If          
   End Sub

   Sub DisplayLoader                   
    results.innerHtml = "<img class=""loader"" src=""[URL unfurl="true"]http://www.dzone.com/images/std/ajax-loader-85.gif"">"[/URL]     
   End Sub

   Sub Main
    If Len(stuacct.value) > 0 Then
     aStuAccts = CollectStudentAccts    
 
     If isArray(aStuAccts) Then     
      iStuAcctTot = UBound(aStuAccts,2)      
      i = 0      
      iTimerID = window.setInterval("DisplayStuAccts",1,"vbscript")
     Else      
      results.innerhtml = "No Recs"
     End If
    Else
     results.innerhtml = "Data Entry Required"
    End If      
   End Sub
   
   Function GetAdsPath
    Set oRootDSE = GetObject("LDAP://RootDSE")
    sRootAdsPath = oRootDSE.Get("defaultNamingContext")
    GetAdsPath = "CN=Users," & sRootAdsPath
   End Function

   Function OpenActDir
    Set oConn = CreateObject("ADODB.Connection")
    oConn.Provider = "ADsDSOObject"
    oConn.Open "Active Directory Provider"
   End Function       
  </script>
 </head>
 <body> 
  Student Account: <input id="stuacct">
                   <input type="button" value="Go" onclick="Main">
                
  <div id="results"></div> 
 </body>
</html>
 
[1] On a html/hta page, synchroneity and display timing can cause problem. As scripted (I refer only to post #1, haven't read the followups), the display of the waiting animation ajax-load=85.gif can be untimely making it purposeless as it won't display until search is all done and by that time another graphic should be displayed.

[2] This is the plan how to proceed.

[2.1] At click, the handler should do the display and use setTimeout to call the search function/sub. Scripted like this, the animation gif will show up before search actually begin. This is resolving the major part of the timing problem mentioned.
[tt]
sub startsearch(sUserInput)
if len (sUserInput)<>0 then 'or more vigorous filtering
DisplayLoader 'display the animation
window.setTimeout("DisplayStudentAccts2(""" & sUserInput & """)", 10)
else
'do nothing and let the handler end a silent death
end if
ens sub
[/tt]
[2.2] The DisplayStudentAccts2(sUserInput) should be scripted similar to DisplayStudentAccts(sUserInput) with a couple of important difference.

[2.2.1] Obviously, the if len(...)<>0 then checking is taken out as it is already placed in the wrapper in [2.1] startSearch().

[2.3] For the main functionality of searching the directory, you should use provision incorporating _asynchoneity_. If you script as what shown, the cpu may be taken up by the search. (A consequence may be the animation drops dead...) The way to search with asynchroneity, refer to this msdn article.

[2.3.1] Asynchronous may require to branch out the function. In that case, the final call to change the graphic may be controlled by some global variable indicating the search is complete. But, this is a detail that may need some experimentation on your part.

[2.4] After the search finish, display the jpg as shown.

[2.5] I would take out the setInterval() part, hence, the clearInterval() as well, in the DisplayStudentAccts() unless the display animation is not animating at all.

[3] The above is the layout of the flow of logic I would suggest.
 
Amendment
Upon re-read what I posted, this line in [2.1] should be read like this, without parentheses.
[tt] window.setTimeout[highlight] [/highlight]"DisplayStudentAccts2(""" & sUserInput & """)", 10[highlight] [/highlight][/tt]
 
Actually the term is dimensioning, not dimming


yes dilettante - however,
it was simply kinda shortcut in terms of an alias on my part...for example: "kinda" meaning "kind of" - not the literal sense - as you apparently figured out ;-)

anyhoot... thanks tsuji ...i'll look into your suggestion and compare against whati had posted - mine works great thus far...but you nebver know! - as always...thanks for the input
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top