Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 运行功能30分钟_Javascript_Timer_Range_Dom Events - Fatal编程技术网

Javascript 运行功能30分钟

Javascript 运行功能30分钟,javascript,timer,range,dom-events,Javascript,Timer,Range,Dom Events,这是我想做的 执行一个函数:一天中的某个时间执行一次 该函数运行30分钟 我尝试过setTimeout,但它不符合我的要求,因为它在X毫秒后运行函数。而我需要立即执行该函数,在所需时间执行30分钟。代码如附件所示 var d = new Date(); var hour = d.getHours(); var minute = d.getMinutes(); var day = self.getDate(); var month_name=new Array(12); month_name

这是我想做的

  • 执行一个函数:一天中的某个时间执行一次
  • 该函数运行30分钟
  • 我尝试过
    setTimeout
    ,但它不符合我的要求,因为它在X毫秒后运行函数。而我需要立即执行该函数,在所需时间执行30分钟。代码如附件所示

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

    使用setInterval函数

    Syntax->var interval=setInterval(function(){function_name()},以毫秒为单位的超时)

    要清除间隔或停止功能,我们使用->清除间隔(间隔)

    HTML //设置函数180000的关闭时间=30分钟。它将隐藏div registration open并显示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');
    });
    

    正在运行演示

    您想在30分钟后终止该功能吗?从函数外部无法执行此操作。您需要在函数本身内部进行检查。请注意,根据您的执行平台,您可能会达到执行长度限制。许多浏览器都提供杀死长时间运行的脚本的功能。至于延迟,如果毫秒计数导致整数溢出,只需将其分解为尽可能长的超时并循环,直到时间正确。一个需要30分钟才能运行的函数?尝试了这个,它就不起作用了。我应该把var间隔放在哪里?//使用jquery 1.91,它是$(document).ready(函数(){//上面所有的代码都在这里});有什么我错过的吗?基本上,只是想在特定的时间显示和隐藏特定的div,但它似乎不起作用。假设已关闭注册,但它仍会在设定的时间打开。
    $(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) {
    
            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');
    });