Update a PHP variable within a Javascript function

Asked

Viewed 946 times

0

I am using the following code to display the current server time:

<p id="demo"></p>
<script>
  function segundoAtual() {
    var n = <?php
	date_default_timezone_set('America/Sao_Paulo');
	$cH = date('G');
	$cM = date('i');
	$cS = date('s');
	echo ($cH*60*60)+($cM*60)+$cS;
	?>;
    	var segundosInicio = inicioYujiaLynxA+(-difLynx);

c = (( n - segundosInicio + duraYujia ) % duraYujia ) / duraYujia;

i = Math.floor((mapYujia.length * 2 - 2) * c);
if( i >= mapYujia.length ) i = mapYujia.length * 2 - i - 1;

var pin = document.getElementById('pin');
pin.style.left = (mapYujia[i][0]/600*100) + '%';
pin.style.top  = (mapYujia[i][1]/757*100) + '%';
  }
    segundoAtual();
    setInterval(segundoAtual, 1);
</script>

The variables segundosInicio, inicioYujiaLynxA and difLynx are defined in another file, called through <script src="servers.js"></script> at the beginning of the document.

The variable segundoAtual defines the position of an element (pin) on a map...

The result is obtained in seconds from midnight tonight.

Either setInterval is not running to update the time, or the variable can only be updated if the page is reloaded?

1 answer

2


Only when the page is updated, ideally would you do the following:

Create a PHP page that returns the server time. Create a second page with your javascript by calling via ajax that page that returns the time. So you can update your javascript element.

test php.

<script   src="https://code.jquery.com/jquery-3.1.1.min.js"   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>
<p id="demo"></p>
<script>
$( document ).ready(function() {
  // Handler for .ready() called.
        update();
});
function update(){
setInterval(
    function() {

$.ajax({
    url: "hora.php",
    success: function(n){
  document.getElementById("demo").innerHTML = n;
    segundoAtual(parseInt(n))
    }
});

    },

1000);
}

    function segundoAtual(n) {

        var segundosInicio = inicioYujiaLynxA+(-difLynx);

c = (( n - segundosInicio + duraYujia ) % duraYujia ) / duraYujia;

i = Math.floor((mapYujia.length * 2 - 2) * c);
if( i >= mapYujia.length ) i = mapYujia.length * 2 - i - 1;

var pin = document.getElementById('pin');
pin.style.left = (mapYujia[i][0]/600*100) + '%';
pin.style.top  = (mapYujia[i][1]/757*100) + '%';
  }

</script>

php time.

<?php
date_default_timezone_set('America/Sao_Paulo');
$cH = date('G');
$cM = date('i');
$cS = date('s');
echo ($cH*60*60)+($cM*60)+$cS;
?>
  • @Nilbervittorazii never used ajax in any project. Could you give me more instructions on how to proceed? Edit: thanks, I’ll try to follow what you’ve been through.

  • @Nilbervittorazii thanks for informing what to do, really updates now. But I was completely lost on how to adapt to the project, before it was just javascript, and the rest of the function I don’t know how to include along with its.

  • @Nilbervittorazii made the issue of the question including all the code I’m using, could you instruct me again? thanks in advance.

  • The variable "N" is being used where in your javascript? I didn’t see anything referencing it.... " var n = <?php"... explain what you want to do at the end of all this...

  • Sorry, I forgot to change the code when I passed here to the question, the "N" is in "c = (( N - secondsInicio + duraYujia ) % duraYujia ) / duraYujia;" and represents the second current so that the "pin" moves on the map according to the server time.

  • updated again, remembering that the code is not complete, I just joined where should be my part along with your..

  • Thank you, it worked perfectly. Parseint is used to join the two functions?

  • parseint is only to ensure that if the result of PHP comes as text it converts to integer, for vc can use as number in its javascript function

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.