jQuery 倒计时不适用于所有版本的 IE

发布于 2024-11-18 17:25:49 字数 840 浏览 1 评论 0原文

我在 Rails 项目之一中使用 jquery 倒计时,代码如下 -

var timeoff = new Date("#{time_format(auction.end_time)}");
$("#myDivId .timerContent").countdown({until : timeoff, onExpiry:applySold, compact : true, layout : '', serverSync: serverTime });

serverTime 方法有 -

 function serverTime(){
    $.get("/server_time","", function(data){
      time = new Date(data);
    }, "text");
  return time;

}

此代码在 Firefox、safari 和 chrome 中运行良好,但在 IE 中不起作用。 在 IE 中,它呈现此 - Nan:NaN:NaN

可能是什么原因导致的?

PS 在上面的代码 i 处 var timeoff = new Date("#{time_format(auction.end_time)}");

我的时间格式为 - “2011-07-07T00:00:00+05:30”

,我的文档类型设置为 -

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

I am using jquery countdown in one of rails project with following code -

var timeoff = new Date("#{time_format(auction.end_time)}");
$("#myDivId .timerContent").countdown({until : timeoff, onExpiry:applySold, compact : true, layout : '', serverSync: serverTime });

and serverTime methods has -

 function serverTime(){
    $.get("/server_time","", function(data){
      time = new Date(data);
    }, "text");
  return time;

}

This code works very well with firefox, safari and chrome but doesn't work in IE.
In IE it renders this - Nan:NaN:NaN

What might be causing this ?

P.S. In above code i at the point of
var timeoff = new Date("#{time_format(auction.end_time)}");

i have time format as - "2011-07-07T00:00:00+05:30"

and my Doc type is set as -

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2024-11-25 17:25:49

我有完全相同的问题,我使用 Date.parse($(".time").html) ,它对 IE 和 FF 都输出良好...当我在纯 HTML 中测试它时,它可以工作并为两个浏览器倒计时! !但它似乎 RUBY rials.js 或 JS 的加载顺序与 IE 冲突..!!!!!

在 IE 和 FIREFIOX 中测试 www.scratch22.net/testing 页面!工作正常!但不是来自实际的 Rails 应用程序!

编辑
因此,我从许多不同的地方尝试了许多不同的答案,但只能得到一个用于在 IE 的 javascript 中比较日期时间和时区的答案

    ruby-1.9.2-p180 :001 > Time.now
        => 2011-07-14 08:55:26 +1000 
    ruby-1.9.2-p180 :002 > Time.now.to_s
        => "2011-07-14 08:55:30 +1000" 
    ruby-1.9.2-p180 :004 > Time.now.to_datetime.to_s 
        => "2011-07-14T08:55:51+10:00"
    ruby-1.9.2-p180 :005 > Time.now.to_formatted_s 
        => "2011-07-14T08:56:18+10:00"
    ruby-1.9.2-p180 :003 > Time.now.to_datetime
        => Thu, 14 Jul 2011 08:55:35 +1000

** 我最初有 <%= Time.now.to_datetime %>并且没有意识到这在打印到页面时返回不同的字符串格式 **

    ruby-1.9.2-p180 :007 > Time.now.to_formatted_s(:rfc822)
        => "Thu, 14 Jul 2011 08:57:26 +1000"

最后一个是我可以让 IE 作为数字使用的唯一带有时区的时间格式...我也摆脱了 Parse ..我

想做的就是返回从现在到价格应该下跌的时间,
并显示倒计时...

生成的 JS 类似于

    now = new Date().getTime(); // Thu, 14 Jul 2011 07:57:26 +1000
    drops_in = new Date("Thu, 14 Jul 2011 08:57:26 +1000").getTime(); //1 hour in future
    countdown = drops - now;
    $(".counter_holder").html(format_time(countdown)); // 00:35:25

每 500 毫秒更新一次的间隔

吸取的教训...

    ruby-1.9.2-p180 :003 > Time.now.to_datetime


<%= Time.now.to_datetime %>

使用 <%= Time.now.to_formatted_s(:rcf822) %>反而

I have the exact SAme ISSUE, im using Date.parse($(".time").html) which outputs fine for both IE and FF... When I test this in plain HTML it works and counts down for BOTH BROWSERS!! But Its seems RUBY rials.js or the load order of the JS's are conflicting with IE..!!!!!

Test this page at www.scratch22.net/testing in BOTH IE and FIREFIOX!! works fine!! but not from within an actual rails app!!

EDIT
So I tried many many different answers from many different places and could only get one to work for comparing datetime with timezone in javascript for IE

    ruby-1.9.2-p180 :001 > Time.now
        => 2011-07-14 08:55:26 +1000 
    ruby-1.9.2-p180 :002 > Time.now.to_s
        => "2011-07-14 08:55:30 +1000" 
    ruby-1.9.2-p180 :004 > Time.now.to_datetime.to_s 
        => "2011-07-14T08:55:51+10:00"
    ruby-1.9.2-p180 :005 > Time.now.to_formatted_s 
        => "2011-07-14T08:56:18+10:00"
    ruby-1.9.2-p180 :003 > Time.now.to_datetime
        => Thu, 14 Jul 2011 08:55:35 +1000

** I originally had <%= Time.now.to_datetime %> and didnt realise that this returns a different string format when printing out to a page **

    ruby-1.9.2-p180 :007 > Time.now.to_formatted_s(:rfc822)
        => "Thu, 14 Jul 2011 08:57:26 +1000"

The last Was the only time format with timezone that I could get IE to work with as a number... I got rid of the Parse aswell...

All up what I wanted to do was return the time between now and when a price should drop,
and show a countdown...

Resulting JS was something like

    now = new Date().getTime(); // Thu, 14 Jul 2011 07:57:26 +1000
    drops_in = new Date("Thu, 14 Jul 2011 08:57:26 +1000").getTime(); //1 hour in future
    countdown = drops - now;
    $(".counter_holder").html(format_time(countdown)); // 00:35:25

inside of a interval updating every 500 milliseconds

The lesson learnt...

    ruby-1.9.2-p180 :003 > Time.now.to_datetime

is not the same as

<%= Time.now.to_datetime %>

Use <%= Time.now.to_formatted_s(:rcf822) %> instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文