Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a javascript function on the given date time

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?

like image 380
Sujan Shrestha Avatar asked Sep 26 '26 02:09

Sujan Shrestha


2 Answers

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

like image 163
2 revsRadonirina Maminiaina Avatar answered Sep 27 '26 15:09

2 revsRadonirina Maminiaina


Compute the number of seconds on server side
use setTimeout(function(){ some_function(); }, nbreOfSeconds);

setInterval() will valuates your expression at specified intervals

like image 25
Halayem Anis Avatar answered Sep 27 '26 14:09

Halayem Anis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!