Pergunta

I'm using countdown.js: http://blog.smalldo.gs/2013/12/create-simple-countdown/

I have no experience with JavaScript, and I need the countdown to be set to a week from now, 7 days, so February 24, 2014.

How would I go about changing this?

Here is what I have in my html:

<head>
<script src="/js/countdown.js"></script>  
</head>

<h1 id="countdown-holder"></h1>  

<script>  
  var clock = document.getElementById("countdown-holder")  
    , targetDate = new Date(2050, 00, 01); // Jan 1, 2050;  

  clock.innerHTML = countdown(targetDate).toString();  
  setInterval(function(){  
    clock.innerHTML = countdown(targetDate).toString();  
  }, 1000);  
</script>  
Foi útil?

Solução

Just change targetDate = new Date(2050, 00, 01);
to targetDate = new Date(2014, 01, 24);
The date constructor takes year, month-1, day.

This is for a fixed date (such as Feb 24 2014), not a relative date (such as "one week from today").

Outras dicas

  var newDate = new Date();
  var numberOfDaysToAdd = 7;
  newDate.setDate(newDate.getDate() + numberOfDaysToAdd); 

  var clock = document.getElementById("countdown-holder")  
    , targetDate = newDate;  

  clock.innerHTML = countdown(targetDate).toString();  
  setInterval(function(){  
    clock.innerHTML = countdown(targetDate).toString();  
  }, 1000); 

This will start counter from 7 days. It will add 7 days to todays date.

try something like this, here is also the js fiddle:

http://jsfiddle.net/rnewsome/D3tPR/

Requires an html element on the page like so:

<div id="counter" />

var StartTime = new Date();
var counter = document.getElementById("counter");
var timeout = null;

function GetCount() {
    var timeToExpire = new Date(StartTime);
    timeToExpire.setDate(timeToExpire.getDate() + 7);

    var ms = timeToExpire.getTime() - new Date().getTime();
    console.log(ms + "ms", (ms/1000) + "s");

    return ms;
};

function UpdateUI() {
    var timeRemaining = parseInt(GetCount() / 1000);
    counter.innerHTML = timeRemaining + " seconds"; 
    if(timeRemaining > 0) {
        timeout = setTimeout(UpdateUI , 1000); // Update Counter every second
    }
}

// Init
UpdateUI();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top