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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Timer fading problem

    Trying to put together a simple javascript code to make a series of text snips fade into and out of visibility in random intervals. It isn't working now, but it's not producing any specific erors to focus debugging on either. Any thoughts on what I'm doing wrong?:
    Code:
    var CommentList = ['Com1','Com2','Com3','Com4','Com5','Com6','Com7','Com8','Com9','Com10','Com11','Com12','Com13','Com14','Com15','Com16'];
    var CommentOn = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    
    function SetOpacity(ComID,Value){
    document.getElementById(ComID).style.opacity = Value;
    }
    function resetOn(OnID){
    CommentOn[OnID] = 0;
    }
    
    function CommentTwinkle(comment){
    document.getElementById(CommentList[comment]).style.visibility=visible;
    CommentOn[comment] = 1;
    for (i = 0; i <= 1; i = i + 0.05) {
        setTimeout("setOpacity(" +CommentList[comment]+","+ i + ")", i * 3000);
        setTimeout("setOpacity(" +CommentList[comment]+","+ (1-i) + ")", 8000+(i * 3000));
        setTimeout(resetOn(comment),12000)
    }
    Rand = 0;
    CueIt=0;
    function CueComment(){
    while (CueIt == 1){
    Rand = math.random()*(CommentList.count);
    CueIt = CommentOn[Rand];
    }
    CommentTwinkle(Rand);
    setTimeout('CueComment()',1000)
    }
    document.body.onload=CueComment();

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    A few things that stand-out immediately are:

    document.body.onload=CueComment();
    should be document.body.onload=CueComment; although it's more usual to attach the handler to window.

    Code:
    function CueComment(){
    while (CueIt == 1){
    Rand = math.random()*(CommentList.count);
    CueIt = CommentOn[Rand];
    }
    CommentTwinkle(Rand);
    setTimeout('CueComment()',1000)
    }
    Nothing ever sets the value of CueIt to 1.

    Code:
    math.random()
    should be Math.random()

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the assistance, the errors pointed out, plus a missing } has definately helped, so now the trouble looks to be down to a one line consideration, though I recall having trouble with it before.
    Code:
    var CommentList = new Array('Com1','Com2','Com3','Com4','Com5','Com6','Com7','Com8','Com9','Com10','Com11','Com12','Com13','Com14','Com15','Com16');
    
    document.getElementById(CommentList[comment]).style.visibility="visible";
    getElementById keeps reading null when I try using an array to simplify defining what the ID is, anyone know what problem I'm missing?

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Can't be certain without seeing it in context, but either comment is out of range, or the elements don't exist at the point of execution.


  •  

    Posting Permissions

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