Dreamfire
Dreamfire

Reputation: 103

Timer in javascript with php elements

Two important parameters have a php file:

$szamol

This specifies the number of seconds to wait . ( Need to start from the current time ): When time is up , call these functions:

$jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz();

I know I can not do it in PHP countdown , but I do not know to pass the two parameters in php to javascript function .

For example, javascript setTimeout () function might be good, but I can not give it to it that these two parameters php .

Upvotes: 0

Views: 294

Answers (4)

Nandan Bhat
Nandan Bhat

Reputation: 1563

Try this, I have named this file to start_timer.php, I hope you have the value $szamol before loading this page. This code waits till

 $szamol

seconds after the page loads and call the functions

 $jatek = new jatek( $_SESSION['id'] );
 $jatek->epuletkesz(); 

This is my code,

 <?php
 if(isSet($_GET['token']))
 {
 if($_GET['token'] == "1")
 {
 //  CALL THE FUNCTIONS NOW
 $jatek = new jatek( $_SESSION['id'] );
 $jatek->epuletkesz();


 }


 }
 ?>

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Title of the document</title>

 </head>

 <body>
 <input type="hidden" id="timer" value="<?php echo $szamol; ?>"/>




<script type="text/javascript">
 var timer_value
 $(document).ready(function()
 {
 timer_value = document.getElementById('timer').value;
 setTimeout(function()
 { 

 location.href="start_timer.php?token=1";

 }, timer_value);

 })

 </script>
 </body>

 </html>

Upvotes: 0

nirmal
nirmal

Reputation: 2180

we can give you solution in jquery or javascript side, but reality is that you can not call your php funtion from front end side. so none of the above answer can help you.

if you have any front end side code then try

<?php
$szamol = 6000;

?>
<script>
var szamol = <?php $szamol ?>; // 6 seconds
var timer = setTimeout(function(){
  //your code here
}, <?php $szamol ?>);
</script>

Upvotes: 0

Adam Kaczmarek
Adam Kaczmarek

Reputation: 146

You can use ajax request.

Just print a setTimeout function with ajax call, for example:

 <?php 
  echo "<script>
   setTimeout(function(){
    //ajax request 
   }, $szamol);
  </script>";
 ?>

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

You can use an ajax request like:

var szamol = 6000; // 6 seconds
var timer = setTimeout(function(){
  $.post('/path/to/my/php/file.php', {}, function(data) {
     // do something with the response
  }, 'json');
}, szamol);

Upvotes: 1

Related Questions