jquery - How to make real time count up with Javascript? -
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title>count up</title> </head> <body> <h1>number 1</h1> <div id="counter"></div> <br> <h1>number 2</h1> <div id="counter_2"></div> <script> var start_date = new date("july 2, 2015 11:00:00"); // put in starting date here var interval = 0.366; // in seconds var increment = 1; // increase per tick var start_value = 7325698160; // initial value when it's start date var count = 0; window.onload = function () { var msinterval = interval * 1000; var = new date(); count = parseint((now - start_date)/msinterval) * increment + start_value; document.getelementbyid('counter').innerhtml = count; setinterval("count += increment; document.getelementbyid('counter').innerhtml = count;", msinterval); } var start_date_2 = new date("july 2, 2015 11:00:00"); // put in starting date here var interval_2 = 0.366; // in seconds var increment_2 = 1; // increase per tick var start_value_2 = 738160; // initial value when it's start date var count_2 = 0; window.onload = function () { var msinterval = interval_2 * 1000; var = new date(); count_2 = parseint((now - start_date_2)/msinterval) * increment_2 + start_value_2; document.getelementbyid('counter_2').innerhtml = count; setinterval("count_2 += increment_2; document.getelementbyid('counter_2').innerhtml = count_2;", msinterval); } </script> </body> </html>
i had count up, worked 1 number. however, when tying add more, did not work well. display second function. me out?? lot!!!!
you overwrite window.onload
handler second one. if want asssign multiple event handlers same event use addeventlistener method:
window.addeventlistener('load', function() { var msinterval = interval_2 * 1000; var = new date(); count_2 = parseint((now - start_date_2) / msinterval) * increment_2 + start_value_2; document.getelementbyid('counter_2').innerhtml = count; setinterval("count_2 += increment_2; document.getelementbyid('counter_2').innerhtml = count_2;", msinterval); });
Comments
Post a Comment