One page with multiple countdown js

Source: Internet
Author: User

JQuery CountDown (CountDown)

It can be used for multiple countdown times on a single page and is convenient to call.

 
 
  1. /*
  2. jquery.countdown.js
  3. */
  4. (function($){
  5. var countdown = function(item, config)
  6. {
  7. var seconds = parseInt($(item).attr(config.attribute));
  8. var timer = null;
  9. var doWork = function()
  10. {
  11. if(seconds >= 0)
  12. {
  13. if(typeof(config.callback) == "function")
  14. {
  15. var data = {
  16. total : seconds ,
  17. second : Math.floor(seconds % 60) ,
  18. minute : Math.floor((seconds / 60) % 60) ,
  19. hour : Math.floor((seconds / 3600) % 24) ,
  20. day : Math.floor(seconds / 86400)
  21. };
  22. config.callback.call(item, seconds, data, item);
  23. }
  24. seconds --;
  25. }else{
  26. window.clearInterval(timer);
  27. }
  28. }
  29. timer = window.setInterval(doWork, 1000);
  30. doWork();
  31. };
  32. var main = function()
  33. {
  34. var args = arguments;
  35. var config = { attribute : 'data-seconds', callback : null };
  36. if(args.length == 1)
  37. {
  38. if(typeof(args[0]) == "function") config.callback = args[0];
  39. if(typeof(args[0]) == "object") $.extend(config, args[0]);
  40. }else{
  41. config.attribute = args[0];
  42. config.callback = args[1];
  43. }
  44. $(this).each(function(index, item){
  45. countdown.call(item, item, config);
  46. });
  47. };
  48. $.fn.countdown = main;
  49. })(jQuery);

Instance used:

 
 
  1. <Script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"> </script>
  2. <Script type = "text/javascript" src = "jquery. countdown. js"> </script>
  3. <Script type = "text/javascript">
  4. $ (Function (){
  5. $ ('Ul '). countdown (function (s, d ){
  6. Var items = $ (this). find ('span ');
  7. Items. eq (0). text (d. total );
  8. Items. eq (1). text (d. second );
  9. Items. eq (2). text (d. minute );
  10. Items. eq (3). text (d. hour );
  11. Items. eq (4). text (d. day );
  12. });
  13. });
  14. </Script>
    • // The value 3344 indicates the total number of seconds.
    • Total-seconds
    • -Seconds
    • -Minute
    • -Hour
    • -Day
    • Total-seconds
    • -Seconds
    • -Minute
    • -Hour
    • -Day
    • Total-seconds
    • -Seconds
    • -Minute
  15. Basically, the name of jquery. countdown. js is the same from jquery, but the content in it is different. Therefore, I use the above Code. (Check whether jquery. countdown. js downloaded from the official website can be used. I did not try it myself. Use the code above to implement multiple countdown times on a page .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.