Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts

    seTimeout works but then just stops

    This is making me mad and I know its some stupid mistake I made cause I was just messing around when I made this. But its supposed to display the images one after another, which it does. Until its on the 3rd image, the timeout works fine for the first two rounds btu the third one it creates an error which I checked in Firefox and its saying tha imageSwap() in the timeout is not a valid function (basically). So I was wondering how come it'll show the first two images but fail on the third? I've tried pretty much everything and its some stupid little error thats messing it all up.

    Code:
    <script type="text/javascript">
    var images=new Array()
    images[0]='image1.jpg'
    images[1]='image2.jpg'
    images[2]='image3.jpg'
    
    var current_image=0;
    
    function imageSwap(){
    document.write('<img src="'+images[current_image]+'" alt="'+images[current_image]+'">')
    current_image=current_image+1
    
    var resetSwap=(current_image==images.length)?current_image=0:"";
    setTimeout("imageSwap()",3000);
    }
    </script>
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    This is wrong:

    var resetSwap=(current_image==images.length)?current_image=0:"";

    you do not set a variable in there and save it to a variable. Use a normal if statement!

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually that doesn't matter. I tried just breaking it down into a normal if statement and even completely removing it and it stills tops on the 3rd round.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've tried removing everything except the document.write("") line just to see if it would keep posting the same image and it did, twice, but on the 3rd time it errored.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Don't delay document.write()

    Your problem is that you're delaying the document.write()

    If you wait too long, the browser will attempt to rewrite the entire page, first by dumping the DOM of the currently displayed [stale] page. A better way to accomplish the desired effect is to periodically change the src attribute or even its visibility property.

    Quote Originally Posted by boxxer03
    This is making me mad and I know its some stupid mistake I made cause I was just messing around when I made this. But its supposed to display the images one after another, which it does. Until its on the 3rd image, the timeout works fine for the first two rounds btu the third one it creates an error which I checked in Firefox and its saying tha imageSwap() in the timeout is not a valid function (basically). So I was wondering how come it'll show the first two images but fail on the third? I've tried pretty much everything and its some stupid little error thats messing it all up.

    Code:
    <script type="text/javascript">
    var images=new Array()
    images[0]='image1.jpg'
    images[1]='image2.jpg'
    images[2]='image3.jpg'
    
    var current_image=0;
    
    function imageSwap(){
    document.write('<img src="'+images[current_image]+'" alt="'+images[current_image]+'">')
    current_image=current_image+1
    
    var resetSwap=(current_image==images.length)?current_image=0:"";
    setTimeout("imageSwap()",3000);
    }
    </script>

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll try that. I was just thinking about that and wondering if it would work, so i'll try it and see what happens but that should be it, because if you remove everything and leave the document.write("") then it still errors. Thanks alot.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, that was it. The one thing I didn't try to fix it. Thanks a lot though to both you guys for your help.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I totally missed the fact you were using document.write

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •