www.webdeveloper.com
Results 1 to 6 of 6

Thread: JavaScript, PHP timer

  1. #1
    Join Date
    May 2014
    Posts
    2

    Angry JavaScript, PHP timer

    Hello. First I`m sorry for my English.

    I looked everywhere but not find an answer. I want to do "loop-timer" - when the timer finishes called another timer.

    e.g.:
    first round - 5min.
    pause between rounds - 1min.
    Second round - again 5 minutes, etc.
    For example in
    HTML Code:
    <div id="countbox1"></div>
    first show first timer and then show the second timer.
    I do not understand much JavaScript. So if you can help me. I have the following code:


    Code:
    <?php
        $delay = time() + 5; //for test
        $Y = date('Y', $delay);
        $m = "0" . (date('m', $delay) - 1) . "";
        $d = date('d', $delay);
        $H = date('H', $delay);
        $i = date('i', $delay);
        $s = date('s', $delay);
        $time = $Y . ',' . $m . ',' . $d . ',' . $H . ',' . $i . ',' . $s;
        ?>
        <script >
          dateFuture1 = new Date(<?php echo $time; ?>);
          function GetCount(ddate, iid) {
            dateNow = new Date();
            amount = ddate.getTime() - dateNow.getTime();
            delete dateNow;
            if (amount <= 0) {
    
              //timer is finish
    
            }
    
            else {
              days = 0;
              hours = 0;
              mins = 0;
              secs = 0;
              out = "";
    
              amount = Math.floor(amount / 1000);
    
              days = Math.floor(amount / 86400);
              amount = amount % 86400;
    
              hours = Math.floor(amount / 3600);
              amount = amount % 3600;
    
              mins = Math.floor(amount / 60);
              amount = amount % 60;
    
              secs = Math.floor(amount);
    
              if (days != 0) {
                out += days + " " + ((days == 1) ? "day" : "days") + ", ";
              }
              if (hours != 0) {
                out += hours + " " + ((hours == 1) ? "hour" : "hours") + ", ";
              }
              if (mins <= 9) {
                out += "0" + mins + " " + ((mins == 1) ? "" : "") + " : ";
              }
              if (mins > 9) {
                out += mins + " " + ((mins == 1) ? "" : "") + " : ";
              }
              if (secs <= 9) {
                out += "0" + secs + " " + ((secs == 1) ? "" : "") + ", ";
              }
              if (secs > 9) {
                out += secs + " " + ((secs == 1) ? "" : "") + ", ";
              }
              out = out.substr(0, out.length - 2);
              document.getElementById(iid).innerHTML = out;
              setTimeout(function() {
                GetCount(ddate, iid)
              }, 1000);
            }
          }
          window.onload = function() {
            GetCount(dateFuture1, "countbox1");
          };
        </script>
    
        <div id="countbox1"></div>
    Someone give me an example?

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Have fun ^_^
    PHP Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Continuous Countdown + Wait</title>
    <script>
    var future = new Date(new Date().getTime() + (5 * 60 * 1000));
    // using PHP here isn't necessary...
    //var future = new Date('<?php echo gmdate('r'time() + (60)); ?>');
    var IID = false;
    var $fn = function()
    {
        countdown('foo');
    };
    function countdown(id)
    {
        var time = future - new Date().getTime(),
            str = '',
            d, h, m, s;

        if (time > 0) {
            time = Math.floor(time / 1000);
            d = Math.floor(time / 86400);
            h = Math.floor((time / 3600) % 24);
            m = Math.floor((time / 60) % 60);
            s = Math.floor(time % 60);
            str += (d > 0) ? d + ' day' + (d > 1 ? 's' : '') : '';
            str += ' ' +h+ ':' +m+ ':' +s+ '';

            if (IID === false)
                IID = setInterval($fn, 1000);
        } else {
            clearInterval(IID);
            IID = false;
            str = 'PAUSED!';
            // 1 min wait...
            setTimeout(function(){
                // update future when this is called, add offset...
                future = new Date(new Date().getTime() + (5 * 60 * 1000));
                $fn.call();
            }, 1 * 60 * 1000);
        }
        document.getElementById(id).innerHTML = str;
    }
    window.onload = $fn;
    </script>
    </head>
    <body>
    <p id="foo"></p>
    </body>
    </html>
    Last edited by ShrineDesigns; 05-21-2014 at 07:55 PM.

  3. #3
    Join Date
    May 2014
    Posts
    2
    Quote Originally Posted by ShrineDesigns View Post
    Have fun ^_^
    thank you very much, but can instead "Pause" display counter

  4. #4
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title></title>
    <script>
    var duration = [5 * 60 * 1000, 1 * 60 * 1000],
    	phase = 0,
    	IID = false,
    	future;
    
    function time()
    {
    	return new Date().getTime();
    }
    function fTime(int)
    {
    	var t = Math.floor(int / 86400),
    		s = '';
    
    	if (t > 0)
    		s += t + ' day' + (t > 1 ? 's' : '') + ' ';
    	t = Math.floor((int / 3600) % 24);
    	s += t + ':';
    	t = Math.floor((int / 60) % 60);
    	s += (t < 10 ? '0' : '') + t + ':';
    	t = Math.floor(int % 60);
    	s += (t < 10 ? '0' : '') + t + '';
    	return s;
    }
    var $fn = function()
    {
    	timer.call(null, 'foo');
    }
    function timer(id)
    {
    	var left = Math.ceil((future - time()) / 1000);
    	document.getElementById(id).innerHTML = fTime(left);
    
    	if (left > -1) {
    		if (IID === false) {
    			IID = setInterval($fn, 1000);
    		}
    	} else {
    		clearInterval(IID);
    		IID = false;
    		future = time() + duration[phase];
    		phase = ~phase & 1;
    		$fn.call(null, id);
    	}
    }
    window.onload = $fn;
    </script>
    </head>
    <body>
    <div id="foo"></div>
    </body>
    </html>

  5. #5
    Join Date
    May 2014
    Posts
    11
    There is a big difference showing between both scripts.

  6. #6
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Quote Originally Posted by sawan20 View Post
    There is a big difference showing between both scripts.
    put repetitive code into functions. and cleaned it up a bit. for some odd reason i cannot edit my previous posts >_>
    forgot to initialize future.
    replace
    Code:
    	future;
    with
    Code:
    	future = time() + duration[0];

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17547 seconds
  • Memory Usage 2,909KB
  • Queries Executed 15 (?)
More Information
Template Usage (36):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates