Jump to content

Javacript Count Down timer


bobtutos

Recommended Posts

Hello every one, today I created the count down timer , but I want to make using OOP style this time. the place where I stuck, is the setTimeout method can't call my class/function every 1000 miliseconds.

Please I need your help, where I mess things up?

Your help is highy appreciate,

Thank you.

 

Bellow is my code.

 

<script type="text/javascript">

window.onload = function(){
function ticTock(ddt){
this.currentDate =new Date();
this.dueDate = new Date(ddt);
this.diffDate = this.dueDate.getTime()-this.currentDate.getTime();

if(this.diffDate<=0){
clearTimeout(timer);
document.write("closed")
}

this.seconds = Math.floor(this.diffDate/1000);
this.minutes = Math.floor(this.seconds/60);
this.hours = Math.floor(this.minutes/60);
this.days = Math.floor(this.hours/24);
this.hours %=24,this.minutes %=60,this.seconds%=60;

this.remainder = this.days+'|'+this.hours+"|"+this.minutes+"|"+this.seconds;

var timer = setTimeout(function(){
ticTock(ddt);
},1000);


}
dueDate = "December 25, 2017 00:01:00";
dueDate2 = "December 25, 2019 00:01:00";

var obj = new ticTock(dueDate);
var obj2 = new ticTock(dueDate2);
document.body.innerHTML = obj.seconds;

}
</script>

Link to comment
Share on other sites

You should have a method to run the counter. Your function is just a constructor to create the object, all it should do is set the properties and things like that. You should have a method called tick or something to do the actual work, and that's the method you would use with setTimeout.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...