Javascript 如何在一个月的每个星期天倒计时?
我在我的网站上有一个倒计时,我想它倒计时到下周日上午11:00,当它是周日上午11:01时,我想它自动倒计时到下周日上午11:00,我想每个月重复 我有这个脚本,但似乎无法让它工作,有人能帮我吗 提前谢谢 这是我的htmlJavascript 如何在一个月的每个星期天倒计时?,javascript,jquery,counter,countdown,Javascript,Jquery,Counter,Countdown,我在我的网站上有一个倒计时,我想它倒计时到下周日上午11:00,当它是周日上午11:01时,我想它自动倒计时到下周日上午11:00,我想每个月重复 我有这个脚本,但似乎无法让它工作,有人能帮我吗 提前谢谢 这是我的html <div class="eventtd"> <div class="nextevt">Next Service Start In:</div> <div class="time" contents
<div class="eventtd">
<div class="nextevt">Next Service Start In:</div>
<div class="time" contents="2" rel="1374397200">
</div> <!-- end time -->
下一个服务开始于:
这是我使用的脚本,现在当它达到0时,它将继续变为负值,而不是重置
jQuery(document).ready(function($) {
if (timerhelp == 'yes') {
var init = setInterval(animation, 100);
}
function animation(){
var deadline1 = $('.time').attr('rel');
var deadline2 = $('.time').attr('contents');
var now = new Date();
now = Math.floor(now / 1000);
now = now + Math.floor(deadline2 * 60 * 60);
var counter1 = deadline1 - now;
var seconds1=Math.floor(counter1 % 60);
if (seconds1 < 10 && seconds1 > 0 ){
seconds1 = '0'+seconds1;
}
counter1=counter1/60;
var minutes1=Math.floor(counter1 % 60);
if (minutes1 < 10 && minutes1 > 0){
minutes1 = '0'+minutes1;
}
counter1=counter1/60;
var hours1=Math.floor(counter1 % 24);
if (hours1 < 10 && hours1 > 0){
hours1 = '0'+hours1;
}
counter1=counter1/24;
var days1=Math.floor(counter1);
if (days1 < 10 && days1 > 0){
days1 = '0'+days1;
}
$('.time').html('<table><tbody><tr><th class="day">'+days1+'</th><th class="day">'+hours1+'</th><th class="day">'+minutes1+'</th><th class="day">'+seconds1+'</th></tr><tr><th>Days</th><th>Hours</th><th>Min</th><th>Sec</th></tr></tbody></table>');
}
});
jQuery(文档).ready(函数($){
如果(timerhelp==“是”){
var init=setInterval(动画,100);
}
函数动画(){
var deadline1=$('.time').attr('rel');
var deadline2=$('.time').attr('contents');
var now=新日期();
now=数学楼层(now/1000);
现在=现在+数学地板(死线2*60*60);
var counter1=死线1-现在;
var seconds1=数学下限(计数器1%60);
如果(秒1<10和秒1>0){
seconds1='0'+seconds1;
}
计数器1=计数器1/60;
var minutes1=数学地板(计数器1%60);
如果(分钟1<10&&minutes1>0){
分钟数1='0'+分钟数1;
}
计数器1=计数器1/60;
var hours1=数学地板(计数器1%24);
如果(小时1<10&&hours1>0){
小时1='0'+小时1;
}
计数器1=计数器1/24;
var days1=数学楼层(计数器1);
如果(第1天<10&&1天>0){
days1='0'+days1;
}
$('.time').html(''+days1+''+hours1+''+minutes1+''+seconds1+'DaysHoursMinSec');
}
});
尝试添加以下内容:
var timerhelp = 'yes';
jQuery(document).ready(function($) {
nextSunday();
if (timerhelp == 'yes') {
var init = setInterval(animation, 900); // did change to 900, no need to calculate it more than once a second :)
}
function animation(){
var deadline1 = $('.time').attr('rel');
var deadline2 = $('.time').attr('contents');
var now = new Date();
now = Math.floor(now / 1000);
now = now + Math.floor(deadline2 * 60 * 60);
var counter1 = deadline1 - now;
var seconds1=Math.floor(counter1 % 60);
if (seconds1 < 10 && seconds1 > 0 ){
seconds1 = '0'+seconds1;
}
counter1=counter1/60;
var minutes1=Math.floor(counter1 % 60);
if (minutes1 < 10 && minutes1 > 0){
minutes1 = '0'+minutes1;
}
counter1=counter1/60;
var hours1=Math.floor(counter1 % 24);
if (hours1 < 10 && hours1 > 0){
hours1 = '0'+hours1;
}
counter1=counter1/24;
var days1=Math.floor(counter1);
if (days1 < 10 && days1 > 0){
days1 = '0'+days1;
}
if(days1<0&&hours1<0&&minutes1<0&&seconds1<0){
nextSunday();
}
$('.time').html('<table><tbody><tr><th class="day">'+days1+'</th><th class="day">'+hours1+'</th><th class="day">'+minutes1+'</th><th class="day">'+seconds1+'</th></tr><tr><th>Days</th><th>Hours</th><th>Min</th><th>Sec</th></tr></tbody></table>');
}
});
function nextSunday(){
var now = new Date().getTime() / 1000; // time in seconds;
var rel = $('.time').attr('rel');
if(parseInt(now)>parseInt(rel)){
var n = parseInt(rel)
while(n<parseInt(now)){
n = n+604800;
}
$('.time').attr('rel',n);
}
}
var timerhelp='yes';
jQuery(文档).ready(函数($){
下周日();
如果(timerhelp==“是”){
var init=setInterval(animation,900);//已更改为900,无需每秒多次计算:)
}
函数动画(){
var deadline1=$('.time').attr('rel');
var deadline2=$('.time').attr('contents');
var now=新日期();
now=数学楼层(now/1000);
现在=现在+数学地板(死线2*60*60);
var counter1=死线1-现在;
var seconds1=数学下限(计数器1%60);
如果(秒1<10和秒1>0){
seconds1='0'+seconds1;
}
计数器1=计数器1/60;
var minutes1=数学地板(计数器1%60);
如果(分钟1<10&&minutes1>0){
分钟数1='0'+分钟数1;
}
计数器1=计数器1/60;
var hours1=数学地板(计数器1%24);
如果(小时1<10&&hours1>0){
小时1='0'+小时1;
}
计数器1=计数器1/24;
var days1=数学楼层(计数器1);
如果(第1天<10&&1天>0){
days1='0'+days1;
}
如果(days1parseInt(rel)){
var n=parseInt(rel)
(n尝试添加以下内容:
var timerhelp = 'yes';
jQuery(document).ready(function($) {
nextSunday();
if (timerhelp == 'yes') {
var init = setInterval(animation, 900); // did change to 900, no need to calculate it more than once a second :)
}
function animation(){
var deadline1 = $('.time').attr('rel');
var deadline2 = $('.time').attr('contents');
var now = new Date();
now = Math.floor(now / 1000);
now = now + Math.floor(deadline2 * 60 * 60);
var counter1 = deadline1 - now;
var seconds1=Math.floor(counter1 % 60);
if (seconds1 < 10 && seconds1 > 0 ){
seconds1 = '0'+seconds1;
}
counter1=counter1/60;
var minutes1=Math.floor(counter1 % 60);
if (minutes1 < 10 && minutes1 > 0){
minutes1 = '0'+minutes1;
}
counter1=counter1/60;
var hours1=Math.floor(counter1 % 24);
if (hours1 < 10 && hours1 > 0){
hours1 = '0'+hours1;
}
counter1=counter1/24;
var days1=Math.floor(counter1);
if (days1 < 10 && days1 > 0){
days1 = '0'+days1;
}
if(days1<0&&hours1<0&&minutes1<0&&seconds1<0){
nextSunday();
}
$('.time').html('<table><tbody><tr><th class="day">'+days1+'</th><th class="day">'+hours1+'</th><th class="day">'+minutes1+'</th><th class="day">'+seconds1+'</th></tr><tr><th>Days</th><th>Hours</th><th>Min</th><th>Sec</th></tr></tbody></table>');
}
});
function nextSunday(){
var now = new Date().getTime() / 1000; // time in seconds;
var rel = $('.time').attr('rel');
if(parseInt(now)>parseInt(rel)){
var n = parseInt(rel)
while(n<parseInt(now)){
n = n+604800;
}
$('.time').attr('rel',n);
}
}
var timerhelp='yes';
jQuery(文档).ready(函数($){
下周日();
如果(timerhelp==“是”){
var init=setInterval(animation,900);//已更改为900,无需每秒多次计算:)
}
函数动画(){
var deadline1=$('.time').attr('rel');
var deadline2=$('.time').attr('contents');
var now=新日期();
now=数学楼层(now/1000);
现在=现在+数学地板(死线2*60*60);
var counter1=死线1-现在;
var seconds1=数学下限(计数器1%60);
如果(秒1<10和秒1>0){
seconds1='0'+seconds1;
}
计数器1=计数器1/60;
var minutes1=数学地板(计数器1%60);
如果(分钟1<10&&minutes1>0){
分钟数1='0'+分钟数1;
}
计数器1=计数器1/60;
var hours1=数学地板(计数器1%24);
如果(小时1<10&&hours1>0){
小时1='0'+小时1;
}
计数器1=计数器1/24;
var days1=数学楼层(计数器1);
如果(第1天<10&&1天>0){
days1='0'+days1;
}
如果(days1parseInt(rel)){
var n=parseInt(rel)
而(n这段代码应始终为您提供下一个星期天:
var now = new Date();
var sunday = new Date();
sunday.setDate(now.getDate() - now.getDay()); // Make Sunday
sunday.setHours(11); // Set 11am
sunday.setMinutes(0);
sunday.setSeconds(0);
sunday.setMilliseconds(0);
if (sunday < now) sunday.setDate(sunday.getDate() + 7); // Make sure it's future
millisecondsLeft = sunday - now;
var now=newdate();
var sunday=新日期();
sunday.setDate(now.getDate()-now.getDay());//创建星期天
星期天。设定时间(11);//设定时间上午11点
星期日。设定分钟(0);
星期日。设置秒(0);
星期日。设定毫秒(0);
如果(sunday
如果sunday
,您只需在每次通过时检查,如果是,则重新计算sunday
(如果有人想知道,如果您在周日下午计算sunday
,则应触发“确保它是未来”行…此代码应始终为您提供下一个周日:
var now = new Date();
var sunday = new Date();
sunday.setDate(now.getDate() - now.getDay()); // Make Sunday
sunday.setHours(11); // Set 11am
sunday.setMinutes(0);
sunday.setSeconds(0);
sunday.setMilliseconds(0);
if (sunday < now) sunday.setDate(sunday.getDate() + 7); // Make sure it's future
millisecondsLeft = sunday - now;
var now=newdate();
var sunday=新日期();
sunday.setDate(now.getDate()-now.getDay());//创建星期天
星期天。设定时间(11);//设定时间上午11点
星期日。设定分钟(0);
星期日。设置秒(0);
星期日。设定毫秒(0);
如果(sunday
如果sunday
,您只需在每次通过时检查,如果是,则重新计算sunday
(如果有人想知道,如果您在周日下午计算sunday
,则应触发“确保它是未来”行。)不幸的是,这似乎不起作用,仍然是负值。问题似乎出在var counter1=deadline1-现在;
使用我的答案来更改deadline1值。请使用Amadan的解决方案,它很漂亮。尝试过了,但仍然是负值。请看这个:您仍然需要进行更改。不幸的是,这似乎没有实现要做到这一点,仍然会得到负值。问题似乎出在var counter1=deadline1-现在;
使用我的答案来更改deadline1值。请使用Amadan的解决方案,它很漂亮。尝试过了,但仍然是负值。请看这个:您仍然需要进行更改。从逻辑上讲,您的代码是合理的,但我似乎无法更改如果它不起作用,我一定是做错了什么。从逻辑上讲,你的代码是完美的