Is there a function similar to setTimeout() (JavaScript) for PHP?
There is no way to delay execution of part of the code of in the current script. It wouldn't make much sense, either, as the processing of a PHP script takes place entirely on server side and you would just delay the overall execution of the script. There is sleep()
but that will simply halt the process for a certain time.
You can, of course, schedule a PHP script to run at a specific time using cron jobs and the like.
Is there a set time out equivalent in php?
You can use the sleep()
function:
int sleep ( int $seconds )
// Delays the program execution for the given number of seconds.
Example:
public function sleep(){
sleep(1);
return 'slept for 1 second';
}
How we can use setTimeout function multiple time in a single page without reloading page
I see that you are refreshing page for every 3 seconds but you have set time out of 10 seconds to hide modal instead of 1 second. This is the reason you cannot see modal hiding even after div refreshed.
I have corrected 10 seconds (10000) to 1 second (1000)and seems like your code is working as expected
CORRECTION: OP need to set time out for 1 min and not 1 second. I am showing modal conditionally.
var modalshown = false;// Code to show popup on new call setInterval(function () { if(!modalshown) { console.log('Modal.show()'); $('#myModal').modal({ backdrop: 'static', keyboard: true, show: true}); modalshown = true; calltimeout(); } //} }, 3000);
function calltimeout(){ setTimeout(function() { console.log("timeout"); $('#myModal').modal('hide'); modalshown = false; }, 100000); // <-- time in milliseconds }
<!DOCTYPE html><html><head><title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script></head><body>
<h1>This is a Heading</h1><p>This is a paragraph.</p>
</body>
<!-- Modal --><div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog">
<!-- Modal content--> <div class="modal-content"> <div class="modal-header text-center"> <h4 class="modal-title modal-title-100">New call by <small id="callertype"></small></h4> </div> <div class="modal-body text-center"> <button type="button" class="btn btn-success col-sm-3 mdlbtn" id="acceptcall" onclick="acceptcall()" data-dismiss="modal"> Accept</button> <button type="button" class="btn btn-danger col-sm-3 mdlbtn" id="declinecall" onclick="declinecall()" data-dismiss="modal">Decline</button> </div> <div class="modal-footer"> </div> </div>
</div></div></html>
setInterval' vs 'setTimeout'
setTimeout(expression, timeout);
runs the code/function once after the timeout.
setInterval(expression, timeout);
runs the code/function repeatedly, with the length of the timeout between each repeat.
Example:
var intervalID = setInterval(alert, 1000); // Will alert every second.
// clearInterval(intervalID); // Will clear the timer.
setTimeout(alert, 1000); // Will alert once, after a second.
Function runs immediately without setTimeout() in Javascript
This function will be invoked immediately. IIFE
(function(){
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
})()
Javascript setTimeout inside setInterval
I guess your problem is that each time you reload your page with setInterval
you are spawning another setTimeout
chain, without clearing the previous one.
Try saving your timeoutId
and clearing it as the first thing in your countdown code. Something like this should work:
<?
$query=q_return("SELECT nume, cod, data, MINUTE(data_start) as minute, data_start FROM chestionare WHERE data_start>date_sub(now(), interval 2 minute)");?>
<script>
var timp_c="<?=$query['minute'];?>";
var sec,
min,
timeoutId;
$.get('<?=return_url("php/getimp.php");?>', { k: "sec" } ).done(function(data){sec=data;});
$.get('<?=return_url("php/getimp.php");?>', { k: "min" } ).done(function(data){min=data;});
if (timeoutId) {
clearTimout(timeoutId);
}
timeoutId = setTimeout(refresh, 1000);
function refresh(){
if(sec>=1){
sec=sec-1;
}else{
min=min-1;
sec=59;
}
if(sec<10){
$("span#ex_start_timp_ramas_sec").each(function(){
$(this).html("0"+sec);
});
}else{
$("span#ex_start_timp_ramas_sec").each(function(){
$(this).html(sec);
});
}
$("span#ex_start_timp_ramas_min").each(function(){
$(this).html(min);
});
if(sec==0 && min==0){
//$("#ex_click_final").trigger('click');
clearInterval(t);
$("#ex_new_test").hide();
}
timeoutId = setTimeout(refresh, 1000);
}
</script>
Related Topics
Php's Function to List All Objects's Properties and Methods
Phpmailer Error: Smtp -> Error: Failed to Connect to Server
Routes in Codeigniter - Automatically
How to Check Whether Suhosin Is Installed
How to Render Content from String/Database with Twig
Sparql Query to Get All Parent of a Node
Are There Different Types of Double Quotes in Utf-8 (Php, Str_Replace)
Laravel Input Facade VS Request Facade
PHP Sort($Array) Returning 1 Instead of Sorted Array
Read the Longest String from an Array in PHP 5.3
Pdo Returning Error "Could Not Find Driver" with a Known Working Dsn
Properly Escaping with MySQLi | Query Over Prepared Statements
Php, Why Do You Escape My Quotes
PHP Upload Progress in PHP 5.4 Is Not Working. Session Variables Not Set
What Do \R and \N Mean in PHP (And Other Languages)
PHP Loop; How to Print Each Result and Delay It for a Second Before Echoing Another Result