0

Here's what I want to do.

  1. Execute a function : once, at some time of the day.
  2. The function run for 30 minutes.

I've tried setTimeout but it doesn't fit my requirement because it run the function after X millisecond. Whereas I need the function to execute right away, at desired time for 30 minutes. Code as attached.

var d = new Date(); 
var hour = d.getHours();
var minute = d.getMinutes();
var day  = self.getDate();

var month_name=new Array(12);
month_name[0]="January"
month_name[1]="February"
month_name[2]="March"
month_name[3]="April"
month_name[4]="May"
month_name[5]="June"
month_name[6]="July"
month_name[7]="August"
month_name[8]="September"
month_name[9]="October"
month_name[10]="November"
month_name[11]="December"

var month = month_name[self.getMonth()];
var fullDate = month+' '+day+' '+hour+':'+minute;

function someFunction() {}

function closeFunction(){
   noticeDiv.css('display', 'block');
   mainDiv.css('display', 'none');
}

function executeFunction(targetDate){
   if (fullDate == targetDate){
     setTimeout ( closeFunction(), 180000 );
   }else{
     someFunction();
   }
}

executeFunction(targetDate);
2
  • 2
    You want to terminate the function after 30 minutes? There is no way to do this from outside the function. You will need to do that checking inside the function itself. Note that depending on your execution platform, you may hit execution length limits. Many browsers offer to kill scripts that run for long periods of time. As for the delay, if the millisecond count is causing integer overflow, simply break it down to the longest possible timeout and loop until the time is right.
    – Unsigned
    Commented Apr 30, 2013 at 4:49
  • A function that takes 30 minutes to run?
    – Bergi
    Commented May 3, 2013 at 12:26

1 Answer 1

0

Use setInterval Function

Syntax-> var interval = setInterval(function(){function_name()},timeout In milliseconds);

To clear Interval or stop function we use ->clearInterval(interval);

HTML

<!-- Hide by default, show at target time -->
<div id="noticeDiv" style="display: none">
        <h2>Registration Closed.</h2>

</div>
<!-- Show by default, hide at target time -->
<div id="mainDiv">
        <h2>Registration Open.</h2>

</div>

jQuery

$(document).ready(function () {
    var d = new Date();
    var hour = d.getHours();
    var minute = d.getMinutes();
    var day = d.getDate();

    var month_name = new Array(12);
    month_name[0] = "January"
    month_name[1] = "February"
    month_name[2] = "March"
    month_name[3] = "April"
    month_name[4] = "May"
    month_name[5] = "June"
    month_name[6] = "July"
    month_name[7] = "August"
    month_name[8] = "September"
    month_name[9] = "October"
    month_name[10] = "November"
    month_name[11] = "December"

    var month = month_name[d.getMonth()];
    var fullDate = month + ' ' + day + ' ' + hour + ':' + minute;
    console.log(fullDate);
    fulldate = 'May 3 17:1';

    function executeFunction(targetDate) {
        x = 0;
        if (fulldate == targetDate) {

//set closing time of function 180000 = 30 min.It will hide div registration open and show registration closed div.

        interval = setInterval(closeFunction, 180000); 
        } else {
            openFunction();
        }
    }

    function openFunction() {
        console.log('Registration is now open')
    }

    function closeFunction() {
        x++;
        $('#mainDiv').append(x);
        if (x == 1) {
            $('#noticeDiv').show();
            $('#mainDiv').hide();
            clearInterval(interval);
        }
    }
    // Execute time
    executeFunction('May 3 17:1');
});

Working Demo http://jsfiddle.net/cse_tushar/8r5T8/

4
  • Tried this, it doesn't work. Where do I put the var interval anyway?
    – baker
    Commented May 2, 2013 at 1:08
  • // use jquery 1.91 with it $(document).ready(function(){ // here all the above code goes }); Commented May 2, 2013 at 5:22
  • anything that I miss? jsfiddle.net/Lwgrn Basically, just want to show and hide certain div at certain time
    – baker
    Commented May 3, 2013 at 8:15
  • Sorry but it doesn't seems to work. Suppose to closed the registration but it open anyway, on time set.
    – baker
    Commented May 6, 2013 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.