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 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.net/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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With