简体   繁体   English

javascript倒计时结束后,如何将网站访问者重定向到另一个URL?

[英]How to redirect a website visitor to another url after javascript countdown ends?

I've been playing with some javascript countdown code from codepen. 我一直在玩一些来自Codepen的javascript倒计时代码。 It's basically some circles with animations and it displays the minutes, seconds and hours. 基本上是一些带有动画的圆圈,它显示分钟,秒和小时。 Since I'm trying to learn more I thought it'd be a good idea to make a countdown website. 由于我想了解更多信息,因此我认为创建一个倒计时网站是个好主意。

Thing is, I'm stuck. 事情是,我被困住了。 Presently, after the clock reaches the set date it starts counting upwards. 目前,时钟到达设定日期后,它将开始向上计数。 What I'd like it to do instead is that after it hits 0 the user gets redirected to let's say Google.com 我想要它做的是,当它达到0后,用户被重定向到了Google.com。

I've been messing with window.location but I can't seem to find where to fit this function. 我一直在搞乱window.location,但似乎找不到适合该功能的位置。

This is the code: 这是代码:

 var ringer = { //countdown_to: "10/31/2014", countdown_to: "Fri Jul 08 2016 10:0:00 GMT-0500 (CDT)", rings: { 'HOURS': { s: 3600000, // mseconds per hour, max: 24 }, 'MINUTES': { s: 60000, // mseconds per minute max: 60 }, 'SECONDS': { s: 1000, max: 60 }, }, r_count: 3, r_spacing: 10, // px r_size: 110, // px r_thickness: 3, // px update_interval: 11, // ms init: function(){ $r = ringer; $r.cvs = document.createElement('canvas'); $r.size = { w: ($r.r_size + $r.r_thickness) * $r.r_count + ($r.r_spacing*($r.r_count-1)), h: ($r.r_size + $r.r_thickness) }; $r.cvs.setAttribute('width',$r.size.w); $r.cvs.setAttribute('height',$r.size.h); $r.ctx = $r.cvs.getContext('2d'); $(document.body).append($r.cvs); $r.cvs = $($r.cvs); $r.ctx.textAlign = 'center'; $r.actual_size = $r.r_size + $r.r_thickness; $r.countdown_to_time = new Date($r.countdown_to).getTime(); $r.cvs.css({ width: $r.size.w+"px", height: $r.size.h+"px" }); $r.go(); }, ctx: null, go: function(){ var idx=0; $r.time = (new Date().getTime()) - $r.countdown_to_time; for(var r_key in $r.rings) $r.unit(idx++,r_key,$r.rings[r_key]); setTimeout($r.go,$r.update_interval); }, unit: function(idx,label,ring) { var x,y, value, ring_secs = ring.s; value = parseFloat($r.time/ring_secs); $r.time-=Math.round(parseInt(value)) * ring_secs; value = Math.abs(value); x = ($r.r_size*.5 + $r.r_thickness*.5); x +=+(idx*($r.r_size+$r.r_spacing+$r.r_thickness)); y = $r.r_size*.5; y += $r.r_thickness*.5; // calculate arc end angle var degrees = 360-(value / ring.max) * 360.0; var endAngle = degrees * (Math.PI / 180); $r.ctx.save(); $r.ctx.translate(x,y); $r.ctx.clearRect($r.actual_size*-0.5,$r.actual_size*-0.5,$r.actual_size,$r.actual_size); // first circle $r.ctx.strokeStyle = "rgba(128,128,128,0.8)"; $r.ctx.beginPath(); $r.ctx.arc(0,0,$r.r_size/2,0,2 * Math.PI, 2); $r.ctx.lineWidth =$r.r_thickness; $r.ctx.stroke(); // second circle $r.ctx.strokeStyle = "rgba(0, 0, 0, 1)"; $r.ctx.beginPath(); $r.ctx.arc(0,0,$r.r_size/2,0,endAngle, 1); $r.ctx.lineWidth =$r.r_thickness; $r.ctx.stroke(); // label $r.ctx.fillStyle = "#ffffff"; $r.ctx.font = '12px Helvetica'; $r.ctx.fillText(label, 0, 23); $r.ctx.fillText(label, 0, 23); $r.ctx.font = 'bold 40px Helvetica'; $r.ctx.fillText(Math.floor(value), 0, 10); $r.ctx.restore(); } } ringer.init(); 

Thanks for your help! 谢谢你的帮助!

采用

document.location.href = "http://www.google.com";

Your callback method (the method to redirect the user) should be called with the go method. 您的回调方法(重定向用户的方法)应使用go方法调用。 In this snippet, 在这个片段中

$r.time = (new Date().getTime()) - $r.countdown_to_time;

the difference between the current time and the countdown time is calculated. 计算当前时间与倒计时时间之间的差。 Your callback method should be invoked when the time difference crosses 0. ie when the current time crosses the countdown time. 当时间差超过0时,即当当前时间超过倒计时时间时,应调用您的回调方法。 Add the following code: 添加以下代码:

$r.time = (new Date().getTime()) - $r.countdown_to_time;
if($r.time >= 0) {
    callbackMethod()
}

Basically, your code appears to be by far more complex than it should (at least to me). 基本上,您的代码似乎比应该的复杂得多(至少对我而言)。

You should have: 你应该有:

  1. A rendering function for the clock's display, 时钟显示的渲染功能,
  2. A countdown function, 倒数功能
  3. A global variable that starts with the total number of (say) seconds and is updated down every time the timer kicks. 全局变量,以(例如)秒总数开始,并在每次计时器启动时向下更新。

Having this, you initiate the global variable to, say, 120 seconds, create the timer (interval) with the countdown function as a callback; 有了这个,您可以启动全局变量,例如120秒,以倒数功能作为回调来创建计时器(间隔)。 within the function, you update the display, update the value of the global variable and check if it is =0. 在函数中,您可以更新显示,更新全局变量的值,并检查其是否为= 0。 If it is, you just kill the timer. 如果是这样,您只需终止计时器即可。 That's it. 而已。

Except for the rendering function, all the other code should not be more than, say, 5 lines. 除渲染功能外,所有其他代码的长度不得超过5行。

Have a span with id="sec" in your markup and put this code in. 在您的标记中包含id =“ sec”的范围,并将此代码放入。

var sec = 10;
var url = "http://www.google.com";
function tick() {
    if (sec) {
        document.getElementById("sec").innerHTML = sec;
        setTimeout(tick, 1000);
        sec--;
    } else {
        document.location.href = url;
    }
}

tick();
function tick(ticker) {
    if (ticker) {
        document.getElementById("sec").innerHTML = ticker;
        setTimeout(function() { 
           tick(--ticker);
        }, 1000);
    } else {
        document.location.href = "http://www.google.com";
    }
}

tick(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访客离开网站后,继续倒数计时器 - Continue Countdown Timer after visitor leaves website JavaScript 重定向到基于 URL 的另一个网站 - JavaScript Redirect to Page Another website based on URL 倒计时后重定向到另一个页面 - Redirect to another page after countdown 将返回的站点访问者重定向到另一个 URL - Redirect the returning site visitor to another URL 倒计时结束后如何停止倒计时计时器刷新 - How to stop countdown timer from refreshing itself after countdown ends 如何在JavaScript中实现倒计时和重定向 - How to implement countdown and redirect in JavaScript 如何在 Razorpay 交易结束后重定向回我的网站。 注意:如果“redirect=true”作为选项传递 - How to Redirect back to my website after transaction ends on Razorpay . Note: if "redirect=true" is passed as an option Javascript-重定向到另一个网站,并在该页面加载后调用Javascript函数 - Javascript - redirect to another website and call Javascript function after that page loads 创建倒数计时器并在倒数归零后重定向到另一页 - Create a countdown timer and redirect to another page after countdown comes to zero 如何将用户从一个 url 重定向到另一个具有 JavaScript 操作(例如添加到购物车)的网站 - How can I redirect users from one url to another website that has a JavaScript action like add to cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM