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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Javascript countdown..

    Hi all,

    This is my first post on a forum regarding help so I'm sorry in advance.

    I currently have a text based MMORPG.

    I am trying to create a countdown timer for my functions(Crime, Car Steal and so on)

    Code:
        window.setTimeout("Tick()", 1000);
    
        function Tick() {
            window.setTimeout("Tick()", 1000);
        }
    	var Timer;
    var TotalSeconds;
    
    
    function CreateTimer(TimerID, Time) {
        Timer = document.getElementById(TimerID);
        TotalSeconds = Time;
        
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    
    function Tick() {
        TotalSeconds -= 1;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    
    function UpdateTimer() {
        Timer.innerHTML = TotalSeconds;
    }
    
    function Tick() {
        if (TotalSeconds <= 0) {
            alert("Available")
            return;
        }
    
        TotalSeconds -= 1;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }
    
    function UpdateTimer() {
        var Seconds = TotalSeconds;
        
    	var Days = Math.floor(Seconds / 86400);
        Seconds -= Days * 86400;
    	
        var Hours = Math.floor(Seconds / 3600);
        Seconds -= Hours * (3600);
    
        var Minutes = Math.floor(Seconds / 60);
        Seconds -= Minutes * (60);
    
    
        var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)
    
    
        Timer.innerHTML = TimeStr + "Organised Robbery!";
    }
    
    
    function LeadingZero(Time) {
    
        return (Time < 10) ? "0" + Time : + Time;
    
    }
    Any help with regards to this would be greatly appreciated.

    Thanks in advance

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Most importantly, you can't have multiple functions with the same name in the same scope. Change that, so the code is sensible enough for us to see what you're trying to do.

    Once you've done that, you might also want to tell us what you expect your code to do vs. what it's actually doing.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply, my knowledge of Javascript = 0.

    Basically I was guided through this by a friend who knows very little.

    There is a crime timer on my rightmenu. I want that to physically tick down instead of only ever changing when the page is refreshed.


  •  

    Tags for this Thread

    Posting Permissions

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