67674

Is my code a performance killer?

Question:

I am showing the value of the timer.php through AJAX in index.php . However I am concern about the performance of this, if it is a server killer if there are 30 people online, and things like this. Do you suggest me some edits?

Thank you.

index.php

<script language='JavaScript'> setInterval( 'SANAjax();', 1000 ); $(function() { SANAjax = function(){ $('#dataDisplay').load('timer.php'); } }); </script> <div id="dataDisplay"></div>

timer.php

function time_difference($endtime){ $days= (date("j",$endtime)-1); $hours =date("G",$endtime); $mins =date("i",$endtime); $secs =date("s",$endtime); $diff="'day': ".$days.",'hour': ".$hours.",'min': ".$mins.",'sec': ".$secs; return $diff; } $future_time = mktime(0, 0, 0, 9, 19, 2011); $now_time = strtotime("+2 hours"); $end_time = $future_time - $now_time; $difference = time_difference($end_time); if ($future_time <= $now_time ) { echo "Date reached"; } else { echo $difference; }; ?>

Answer1:

Depends on your server specs and number of clients, this could quickly become a server-killer.

The multiple calls to a file every second will quickly put a lot of load for nothing though, so best practice calls for using a javascript timer countdown. I particularly like this one: <a href="http://stuntsnippets.com/javascript-countdown/" rel="nofollow">http://stuntsnippets.com/javascript-countdown/</a>

And for the jQuery implementation:

<script type="text/javascript"> var myDate = new Date(); //Retrieve actual date myDate.setTime(this.getTime() + (3600 * 2)); //Add two hours $(document).ready(function() { $("#time").countdown({ date: myDate.toGMTString(), onComplete: function( event ){ $(this).html("completed"); }, leadingZero: true }); }); </script> <p id="time" class="time">

This should be enough, no more need for PHP calls and the client does everything.

Answer2:

By setting a setInterval for every second, you are basically saying that for 30 clients, you will be getting roughly 30 txn per second to your php server. Its hard to say its a performance killer outside of just saying you will have to handle 30tps for this simple call. I would first quesiton why you are doing this with a server side script. You could just as easily give the html file a datetime when the page loads and do the countdown with just javascript in the browser.

Answer3:

I use jQuery Countdown <a href="http://keith-wood.name/countdown.html" rel="nofollow">http://keith-wood.name/countdown.html</a> to put a timer on my pages to let the user know when they are going to be logged out of the system due to inactivity.

You can bind a function to the counter expiring event.

<a href="http://code.google.com/p/jquery-countdown/" rel="nofollow">http://code.google.com/p/jquery-countdown/</a> ...Simple example

<a href="http://keith-wood.name/countdown.html" rel="nofollow">http://keith-wood.name/countdown.html</a> ...shows off all the bells and whistles...

It can be as simple as the first one or as detailed as the second!

-=Bryan

Recommend

  • Python check if time is in range between two days
  • Partion 20 min slot of time in php from start and end time
  • PHP Countdown Timer Problem?
  • display todays bookings from mysql mrbs calendar
  • strtotime() does not return correct value when specifying date in dd/mm/yyyy format
  • Most efficient way to convert a ISO Date into Unix timestamp?
  • Converting mysql date from one format to another
  • strtotime() converts a non existing date to another date
  • Show a link in fpdf
  • wait function that uses while
  • update trigger to update records in another table
  • JMeter pre-processed variable as part of report file name
  • How to check if one div overlapped another during transition from one position to another
  • When is InputStream.available() useful?
  • Dropping support for JRE 1.3
  • Domain name setup with AWS S3 bucket with static hosting
  • Timer once a minute on the minute
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Running sp_executesql query expects parameter @statement
  • How to get list of users who's birthday is today in MongoDB
  • jqPlot date axis - bars drawn on wrong day?
  • MySql - get days remaining
  • Returning this from a constructor function in JS
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • What is the use of a session store?
  • How to get the date of next specified day of week
  • Put value at centre of bins for histogram
  • Java color detection
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • OOP Javascript - Is “get property” method necessary?
  • Scrapy recursive link crawler
  • Illegal mix of collations for operation for date/time comparison
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query
  • Converting MP3 duration time