Sujan Shrestha
Sujan Shrestha

Reputation: 614

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?

Upvotes: 3

Views: 3794

Answers (6)

ismnoiet
ismnoiet

Reputation: 4169

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.com/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>

Upvotes: 1

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

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

Upvotes: 1

Hayko Koryun
Hayko Koryun

Reputation: 1244

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);
}

Upvotes: 1

bitifet
bitifet

Reputation: 3679

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));

Upvotes: 1

Prasanga
Prasanga

Reputation: 1038

You can use this javascript method. convert your given time as milliseconds.

setInterval(function(){ alert("Hello"); }, 3000);

Upvotes: 1

Halayem Anis
Halayem Anis

Reputation: 7805

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

setInterval() will valuates your expression at specified intervals

Upvotes: 1

Related Questions