www.webdeveloper.com
Results 1 to 6 of 6

Thread: Renew server time <?php echo microtime();?> in pure JavaScript?

Hybrid View

  1. #1
    Join Date
    Mar 2014
    Posts
    7

    Post Renew server time <?php echo microtime();?> in pure JavaScript?

    Ayjax working version:
    <?php echo microtime();?>

    http://lanother.tk/msec.php :

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
    function update(){
    $('body').load('/eho.php');
    }
    update();
    setInterval(function(){ update() }, 1);
    </script>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    What is the question?

    Are you asking if this example can be done with just javascript (no PHP and AJAX)? If that's the case then the answer is simply no. Javascript is client-side and thus can never get server information without the use of AJAX (combined with a server-side language, such as PHP).

  3. #3
    Join Date
    Mar 2014
    Posts
    7
    No jQuery. Don't know it's ajax or ajax is apart and require no inclusions of needless code.

    If ajax is what I write with no superfluous code insertions - so let it be. But no jQuery - I see how mush of generated .js file occupy.

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    Okay, so if I understand this correctly you are wanting to do this same thing but only using standard javascript and no jQuery (because you don't need all of the extra jQuery code).

    This should do the same thing, but with no extra files (jQuery) included.
    HTML Code:
    <!doctype html>
    <html>
    <head>
    	<title>Javascript/AJAX Microtime Update</title>
    	<script>
    		function update() {
    			if(window.XMLHttpRequest) { var $oAjax = new XMLHttpRequest();
    			} else { var $oAjax = new ActiveXObject("Microsoft.XMLHTTP"); }
    			if($oAjax !== null && $oAjax !== undefined) {
    				$oAjax.onreadystatechange = function() {
    					if($oAjax.readyState == 4) document.body.innerHTML = $oAjax.responseText
    				}
    				$oAjax.open("GET", "/eho.php", true);
    				$oAjax.send(null);
    			}
    		}
    		window.onload = function(){
    			update();
    			setInterval(function(){ update() }, 1);
    		};
    	</script>
    </head>
    <body>
    
    </body>
    </html>

  5. #5
    Join Date
    Mar 2014
    Posts
    7
    Quote Originally Posted by Sup3rkirby View Post
    ..
    Only your code works.

    I was also provided with non-working code. They say "enough for browsers comtemporary (современных)"

    01 var request = new XMLHttpRequest,
    02 textNode = document.body.appendChild(document.createTextNode(""));
    03
    04 function update() {
    05 request.open("GET", "/eho.php");
    06 request.send();
    07 }
    08 request.onload = function () {
    09 textNode.nodeValue = request.responseText;
    10 update();
    11 };
    12
    13 update();


    Sup3rkirby,

    It will be a good starting point of starting ajax from
    window.XMLHttpRequest
    new XMLHttpRequest()(
    "
    Microsoft.XMLHTTP
    ")
    if !== null && undefined
    onreadystatechange = function() { if(
    readyState == 4
    document.body.innerHTML
    .respondeText
    .open("GET", "/eho.php", true)
    ;
    .send(null);
    window.onload
    setInterval


    working code.

    Now I sent all queries (ahowever lots of water in googling and smth ..dunno).

    Thank you.
    The point is solved.

  6. #6
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    no needless code
    Code:
    if(typeof XMLHttpRequest === "undefined"){
    	XMLHttpRequest = function(){
    		try {
    			return new ActiveXObject("Msxml2.XMLHTTP.6.0");
    		}catch(e){
    		}
    		try{
    			return new ActiveXObject("Msxml2.XMLHTTP.3.0");
    		}catch(e){
    		}
    		try{
    			return new ActiveXObject("Microsoft.XMLHTTP");
    		}catch(e){
    		}
    	};
    }
    function request(url, callbackFn){
    	var http, ID;
    
    	try{
    		http = new XMLHttpRequest();
    	}catch(e){
    		return false;
    	}
    	http.onreadystatechange = function(){
    		if(http.readyState == 4){
    			clearTimeout(ID);
    			callbackFn(http);
    		}
    	};
    	ID = setTimeout(function(){
    		if(http.readyState != 4){
    			http.abort();
    			callbackFn({
    				responseText: null,
    				status: 408,
    				statusText: "Request Timeout"
    			});
    		}
    	}, 5000);
    	http.open("GET", url, true);
    	http.send();
    	return true;
    }
    function update(){
    	var r = request('/eho.php', null, function(http){
    		if(http.status == 200)
    			document.body.innerHTML = http.responseText;
    	});
    
    	if(!r){
    		//alert('XMLHttpRequest is not accessible.');
    	}else{
    		uID = setTimeout($fn, 1);
    	}
    }
    var $fn = function(){
    	update();
    }, uID;
    update();
    Last edited by ShrineDesigns; 04-12-2014 at 02:21 PM.

Thread Information

Users Browsing this Thread

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

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



Recent Articles