Skip to content

Instantly share code, notes, and snippets.

@creativeaura
Created April 10, 2018 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativeaura/d56fe2941bd2dfc4f9760b4c832da187 to your computer and use it in GitHub Desktop.
Save creativeaura/d56fe2941bd2dfc4f9760b4c832da187 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/pitecoj
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
<style id="jsbin-css">
body,
html {
display: flex;
justify-content: center;
height: 100%;
align-items: center;
}
.bg {
background: #0a2f64;
width: 300px;
height: 100px;
paddinG: 16px;
border-radius: 5px;
color: white;
font-size: 18px;
text-align: center;
}
.bg a {
color: white;
text-decoration: underline;
font-size: 20px;
}
.bg .timmer {
font-weight: bolder;
}
</style>
</head>
<body>
<div id="counterContainer" class="bg">
<div data-bind="visible: showTimer()">
<p data-bind="with: remainingTime">2 tickets reserved for <span class="timmer" data-bind="text: computed"></span></p>
<a href="#">Continue with purchase</a>
</div>
<div data-bind="visible: !showTimer()">
<p>You have lost your reserved tickets</p>
</div>
</div>
<script id="jsbin-javascript">
var startTime = new Date().getTime();
var endTime = new Date("2018-04-10 09:39:26").getTime();
function calculateTimeRemaining(endTime) {
var now = new Date().getTime();
var distance = endTime - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var output = days ? days + "d " : "";
output += hours ? hours + "h " : "";
output += minutes ? minutes + "m " : "";
output += seconds ? seconds + "s " : "";
return {
computed: output,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
}
function CheckoutCounter(endDateTime) {
this.endDateTime = endDateTime;
this.remainingTime = ko.observable();
this.showTimer = ko.observable(true);
this.updateCounter = function() {
this.remainingTime(calculateTimeRemaining(endDateTime));
console.log(this.remainingTime());
this.checkWarning();
};
this.timmer = setInterval(function() {
this.updateCounter();
}.bind(this), 1000);
this.checkWarning = function() {
var r = this.remainingTime();
if (r.days <= 0 && r.hours <= 0 && r.minutes <= 0 && r.seconds <= 0) {
this.showTimer(false);
}
};
}
ko.applyBindings(new CheckoutCounter(endTime));
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"><\/script>
</head>
<body>
<div id="counterContainer" class="bg">
<div data-bind="visible: showTimer()">
<p data-bind="with: remainingTime">2 tickets reserved for <span class="timmer" data-bind="text: computed"></span></p>
<a href="#">Continue with purchase</a>
</div>
<div data-bind="visible: !showTimer()">
<p>You have lost your reserved tickets</p>
</div>
</div>
</body>
</html>
</script>
<script id="jsbin-source-css" type="text/css">body,html {
display: flex;
justify-content: center;
height: 100%;
align-items: center;
}
.bg {
background: #0a2f64;
width: 300px;
height:100px;
paddinG: 16px;
border-radius: 5px;
color: white;
font-size: 18px;
text-align: center;
a {
color: white;
text-decoration:underline;
font-size: 20px;
}
.timmer {
font-weight: bolder;
}
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var startTime = new Date().getTime();
var endTime = new Date("2018-04-10 09:39:26").getTime();
function calculateTimeRemaining(endTime) {
var now = new Date().getTime();
var distance = endTime - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var output = days ? days + "d " : "";
output += hours ? hours + "h " : "";
output += minutes ? minutes + "m " : "";
output += seconds ? seconds + "s " : "";
return {
computed: output,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
}
function CheckoutCounter(endDateTime) {
this.endDateTime = endDateTime;
this.remainingTime = ko.observable();
this.showTimer = ko.observable(true);
this.updateCounter = function() {
this.remainingTime(calculateTimeRemaining(endDateTime));
console.log(this.remainingTime());
this.checkWarning();
};
this.timmer = setInterval(function() {
this.updateCounter();
}.bind(this), 1000);
this.checkWarning = function() {
var r = this.remainingTime();
if (r.days <= 0 && r.hours <= 0 && r.minutes <= 0 && r.seconds <= 0) {
this.showTimer(false);
}
};
}
ko.applyBindings(new CheckoutCounter(endTime));
</script></body>
</html>
body,
html {
display: flex;
justify-content: center;
height: 100%;
align-items: center;
}
.bg {
background: #0a2f64;
width: 300px;
height: 100px;
paddinG: 16px;
border-radius: 5px;
color: white;
font-size: 18px;
text-align: center;
}
.bg a {
color: white;
text-decoration: underline;
font-size: 20px;
}
.bg .timmer {
font-weight: bolder;
}
var startTime = new Date().getTime();
var endTime = new Date("2018-04-10 09:39:26").getTime();
function calculateTimeRemaining(endTime) {
var now = new Date().getTime();
var distance = endTime - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var output = days ? days + "d " : "";
output += hours ? hours + "h " : "";
output += minutes ? minutes + "m " : "";
output += seconds ? seconds + "s " : "";
return {
computed: output,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
}
function CheckoutCounter(endDateTime) {
this.endDateTime = endDateTime;
this.remainingTime = ko.observable();
this.showTimer = ko.observable(true);
this.updateCounter = function() {
this.remainingTime(calculateTimeRemaining(endDateTime));
console.log(this.remainingTime());
this.checkWarning();
};
this.timmer = setInterval(function() {
this.updateCounter();
}.bind(this), 1000);
this.checkWarning = function() {
var r = this.remainingTime();
if (r.days <= 0 && r.hours <= 0 && r.minutes <= 0 && r.seconds <= 0) {
this.showTimer(false);
}
};
}
ko.applyBindings(new CheckoutCounter(endTime));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment