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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple WShell question

Status
Not open for further replies.

lazyrunner50

Programmer
Joined
Jul 30, 2004
Messages
63
Location
US
Ok, I think I've almost worked out all the problems with my webpage, but right now, I'm trying to implement a counter for a slideshow which will wait for 5 seconds and then move to the next image. Basically what I'm doing is querying my database, getting the image names, and putting them into an array. I then pass the array in a session variable along with a counter. I pause for 5 seconds, increment the counter, reload the page, and display the new image. I'm currently having problems with the pause part of it.

Code:
Dim wsh
set wsh = Server.CreateObject("WScript.Shell")
If Session("imageCounter") <> "" Then
theImages=Session("theImages")
counter=Session("imageCounter")
Response.Write ("<table>")
response.write("<tr><td><img src='" & theImages(counter) & ".jpg'></td></tr>")
Response.Write ("</table>")
counter=counter+1
Session("imageCounter")=counter
wsh.echo "Started"
'wsh.Sleep 5000
'Response.Redirect("SlideShowPage2Backup.asp")

I've gotten this error:
Code:
Microsoft VBScript runtime error '800a01b6' 

Object doesn't support this property or method: 'echo' 

SlideShowPage2Backup.asp, line 25

I originally tried sleep, but that didn't work, so then I tried echo, only to get the same results..
 
You don't want to use those methods from inside an ASP page. You can make sleep work, but then your pausing an entire web page in your web server for 5 seconds - this means the web server cannot release any of the memory or resources it was using while executing the page because, technically, the page is sitll executing. This also means that if buffering is turne on (which it is by default) that the page won't be sent to the user right away. Even if you turn buffering off, the users' browser will be told by the web serverthat there is more coming, so it will be in a "loading" state until you finally stop your script or they change pages.

Generally it is much better to do things like this client-side. You can create a javascript that will refresh the .src attribute of an img tag every 5 seconds fairly easily. Then all you would need to do is write your image names into a javascript block as part of an array and your other function could loop through that array, changing the .src proprty every 5 seconds to the next element in the array.

If you absolutely cannot use client-side scripting then your best solution is to put a meta refresh tag in the page and let it refresh the page every 5 seconds, thus getting a new image. If your array is in a session variable then you could also put your counter in a session variable and display the next image in the array every time they refresh by incrementing the counter.

-T

barcode_1.gif
 
Can you pass variables (arrays) from ASP to javascript? If, so then I would be able to do what you are suggesting. Also, the temporary page I have put up to test out this stuff already has a counter that will go through the array with each page refresh...but there are around 450 pictures, so it'd be a ton of refreshing :)
 
>Can you pass variables (arrays) from ASP to javascript?

This is how.
[tt]
<%
dim avb(3)
for i=0 to 3
avb(i)="pic" & i & ".gif"
next
%>
<!-- on client-side content -->
<script language="javascript">
//like this to be global, else adjust the placement of ajs
var ajs=new VBArray(<%=avb%>).toArray();
</script>
[/tt]
 
Sorry! Ignore my above post for the moment. Not as my expectation in that construction!

Instead take an easier path by join and split!
[tt]
<%
dim avb(3), savb
for i=0 to 3
avb(i)="pic" & i & ".gif"
next
savb=join(avb,",") 'or some safe separator
%>
<!-- on client-side content -->
<script language="javascript">
//like this to be global, else adjust the placement of ajs
var ajs="<%=savb%>".split(/,/);
[/tt]
That would be the easiest path to take.
 
Geez...wow..ok. Wasn't thinking for a sec there. Response.write can put anything into the html (or javascript) code... Thanks. I'll have to try that out.
 
Yeah, it surprised me the first time I saw it too. It seems like it would be hard to do until you see it, than you can't figure out why you never realized how easy it was :)

450 pictures seems like a lot to slideshow through. If you want an option other than refreshing then I would go with the method that has a piece of javascript that just canges te src attribute of a given image tag. This way you won't have to refresh the page.

Something like:
Code:
<%
'pretend we want to slideshow all the images in the "slideshow" folder 
' - I'm just doing this so I have a realistic set of image filenames to dump into the javascript

'get a handle on the folder
Dim fso, fol, fil
Set fso = Server.CreateObject("FileSystemObject")
Set fol = fso.GetFolder(Server.MapPath("slideshow"))

'start outputting my HTML
%>
<html>
<head>
<title>My Slideshow!</title>
<script language="javascript">
   var curImageIndex = 0;
   var runSlideshow = true;
   function AdvanceSlideshow(numSeconds){
      if(runSlideshow){
         'advance the image
         curImageIndex++;
         document.getElementbyId("MyImage").src = slideshowImages[curImageIndex];

         'do a setTimeout to advance for the next image
         setTimeout('AdvanceSlideshow(' + numSeconds + ')',numSeconds * 1000);
      }
      else{
         'runSlideshow is false so we don't continue
      }
   }

   var slideshowImages = new Array(<%

   'now we fill the array with addresses to the images
   Dim didFirstFile : didFirstFile = false
   For fil in fol.Files
      If InStr(fil.Name,".jpg") > 0 Or InStr(fil.Name,".gif") > 0 Then
         'only want commas before 2nd, 3rd, 4th, etc files
         If didFirstFile Then 
            Response.Write ","
         Else
            didFirstFile = true
         End If

         'output the path with single quotes around it so javascript will see it as a string
         Response.Write "'slideshow/" & fil.Name & "'"
      End If
   Next

   Set fil = Nothing
   Set fol = Nothing
   Set fso = Nothing
   %>);
</script>
</head>
<body onLoad="AdvanceSlideshow(5);">
<img src="" id="MyImage" alt="Loading Image..." />
<input type="button" onClick="runSlideshow=!runSlideshow; runSlideshow?this.value='Pause Slideshow':this.value='Resume Slideshow';" value="Pause Slideshow" />
</body>
</html>

I threw in some extra code to pause the slideshow off a boolean variable. With some additional code you could create more commands tat would run from javascript since you already have the important part, the array of image paths. You could also make it use a configurable time instead of hardcoded one, have a restart button, etc.

Not sure if that would work out ofthe box or not, since I wrote it on the fly, but it should be accurate enough to show you what I was aiming for when I mentioned letting the javascript do the work with an image tag.

-T

barcode_1.gif
 
Tarwin, thanks for all your help, but I'll have to change up the code quite a bit... I have the pictures organized by city, want them displayed in a certain order, and have included comments with them, so that's why I put their names in a database. I can just select the images which have a certain city code, and since they don't change order in the database, I can pull up just the ones I need in the order I need along with their description. I am thinking about pulling the info out in a two dimensional array, and then passing it to javascript, or if that is too hard, just pull out as two one dimensional arrays, and display the same element of array.
 
That sounds good. I knew that my example wouldn't fit your particular situation, I was hoping it would prove useful as an example (and it sounds like it may have).

Let us know if you run into any more difficulties,

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top