开发者

Fix JQuery Countdown Timer in Rails 3 Application

开发者 https://www.devze.com 2023-03-06 03:37 出处:网络
I need a countdown timer for a rails application I\'m working on so I decided to use this. http://keith-wood.name/countdown.html

I need a countdown timer for a rails application I'm working on so I decided to use this.

http://keith-wood.name/countdown.html

In the public/javascripts folder I've added jquery.countdown.js In the public/styleheets folder I've added jquery.countdown.css

In app/views/layouts/application.html.erb I add the javascript and css files to the stylesheet and javascript tags.

In my view I've added

<script>
    var element = '#countDownTimer';
    var year = <%= @deal.end.year %>;
    var month = <%= @deal.end.month %>;
    var day = <%= @deal.end.day %>;
   setCountdownTimer(element, year, month, day);
</script>
<div id="countDownTimer"></div>

which is going to be used to select with jQuery to add the timer.

Here is my application.js file

// The following function is called once the rest of the document is fully loaded
 $(document).ready(function()
 {  

function setCountdownTimer(elemen开发者_开发问答t, year, month, day) {
  var date = new Date(year, month, day);
  $(element).countdown({until: date});
}


// DEALS TABS ON DEALS PAGES
$("#tabs > ul").tabs(); // Make the tabs.

// FANCYBOX FOR GALLERY IMAGES POPUP
$("a.galleryitem").fancybox();

// MEDIA ENCODER FOR PODCASTS
$.mb_audioEmbedder.playerPath="/media/player.swf" //the path to audio player
    $.mb_videoEmbedder.defaults.width=500;
$.mb_audioEmbedder.defaults.width=214;
    $("#media").mb_embedMovies();
    $("#media").mb_embedAudio();

});

Can anyone tell me why this doesn't work?


Try wrapping

   var element = '#countDownTimer';
   var year = <%= @deal.end.year %>;
   var month = <%= @deal.end.month %>;
   var day = <%= @deal.end.day %>;
   setCountdownTimer(element, year, month, day);

inside a $(document).ready()

also I don't think this needs to be in the $(document).ready()

function setCountdownTimer(element, year, month, day) {
  var date = new Date(year, month, day);
  $(element).countdown({until: date});
}

The issue is probably that your calling setCountdownTimer before its defined

0

精彩评论

暂无评论...
验证码 换一张
取 消