admin管理员组文章数量:1399297
I want to run some specific javascript function
on the given date/time only once.
For example I have a date time variable in php
as below.
$date = "May 7, 2015 17:08:43";
I have some function like
function some_function()
{
.....
.....
}
How can I run this function only once on the given date time May 7, 2015 17:08:43
.
I tried this but it's not working correctly.
var date = "<?php echo date('n/j/Y/H:i', strtotime($date));?>";
var myDate = new Date();
var current_date = (myDate.getMonth()+1) + "/" + myDate.getDate() + "/" + myDate.getFullYear()+ "/"+ myDate.getHours() + ":" + myDate.getMinutes();
var interval = setInterval(function() {
if(date==current_date)
{
some_function();
}
}, 4000);
clearInterval(interval);
Is there any good technique for this?
I want to run some specific javascript function
on the given date/time only once.
For example I have a date time variable in php
as below.
$date = "May 7, 2015 17:08:43";
I have some function like
function some_function()
{
.....
.....
}
How can I run this function only once on the given date time May 7, 2015 17:08:43
.
I tried this but it's not working correctly.
var date = "<?php echo date('n/j/Y/H:i', strtotime($date));?>";
var myDate = new Date();
var current_date = (myDate.getMonth()+1) + "/" + myDate.getDate() + "/" + myDate.getFullYear()+ "/"+ myDate.getHours() + ":" + myDate.getMinutes();
var interval = setInterval(function() {
if(date==current_date)
{
some_function();
}
}, 4000);
clearInterval(interval);
Is there any good technique for this?
Share Improve this question edited May 8, 2015 at 6:03 Sujan Shrestha asked May 6, 2015 at 10:29 Sujan ShresthaSujan Shrestha 6143 gold badges13 silver badges34 bronze badges 3-
i need to know what
some_function()
will do – Halayem Anis Commented May 6, 2015 at 10:33 - every 4000 can skip over the minute you want.... – David Commented May 6, 2015 at 10:34
-
here
some_function
is used to reload the page usinglocation.reload();
– Sujan Shrestha Commented May 6, 2015 at 10:34
6 Answers
Reset to default 1I think, you can do as following:
setInterval(function() {
var myDate = new Date();
$date = "May 06 2015 13:50:40";
var reg = new RegExp('\(' + $date.replace(/\s/g, "\\s") + '\)', 'g');
console.log(myDate)
if ( myDate.toString().match($date) ) {
do_something();
}
}, 1000);
here the demo: http://jsfiddle/pj1248xq/
Note: You need to change the date according to real date you want to execute your function
Compute the number of seconds on server side
use setTimeout(function(){ some_function(); }, nbreOfSeconds);
setInterval()
will valuates your expression at specified intervals
You can use this javascript method. convert your given time as milliseconds.
setInterval(function(){ alert("Hello"); }, 3000);
function runAt (cbk, timestamp) {
if (
! (timestamp instanceof Date)
|| isNaN (timestamp.getTime())
) throw "timestamp should be a valid Date object";
var ms = timestamp - (new Date()).getTime();
if (ms < 0) throw "Too late!!";
setTimeout(cbk, ms);
};
You can test it "any time" with:
runAt(function(){console.log("Hello!!");}, new Date(new Date().getTime() + 5000));
There's a small library that's useful for this over at github
Native setTimeout functions are limited to INT32 (2147483647 milliseconds or roughly 24.8 days). This library makes it possible to set timeouts with a nearly unlimited delay.
Once you've included the library, you can set the timeout like so:
// extra zeros need to get the time in milliseconds (as in JavaScript)
var date = <?php echo strtotime($date);?>000;
var id = timeout.set(some_function, date);
function some_function()
{
// do some cool stuff
timeout.clear(id);
}
You have to use setTimeout function instead of setInterval as @Halayem said because setTimeout will be executed once after the interval is over
where setInterval will be executed and then wait period(nbreOfSeconds) then loop again
okay you can make a ajax call using Jquery(the easy option ) and in your server side language check for the date and then return a json status object for example
here is a simple example :
check_date.php content(this file is used to check for the date):
<?php
$date = date('n/j/Y/H:i', strtotime($date));
if($date == 'your wanted date'){
return json_encode(array('status'=>1));
}else{
return json_encode(array('status'=>0));
}
?>
and this is your JavaScript code : of course you should load jquery first
//here is a jquery cdn link :
<script src="https://code.jquery./jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function(){
$.('check_date.php',{},function(serverRespond){
jsonRespond = JSON.parse(serverRespond);
if(jsonRespond.status === 1){
setTimeout(function(){
// here is your js function that you want to execute
},afterMilSeconds);
}
});
});
</script>
本文标签: phphow to run a javascript function on the given date timeStack Overflow
版权声明:本文标题:php - how to run a javascript function on the given date time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744210523a2595396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论