Javascript周计划功能

时间:2014-05-30 12:58:24

标签: javascript html function schedule

我的商店里安装了安全摄像头。我希望它能安排在整个星期的不同时间转到特定预设。

我目前的代码如下:

<html>
<body>
<SCRIPT type='text/javascript'language='javascript'>
var IPandPort = "ip:port
var user = "user"
var pwd = "pass"
var poPreset = "31"; 
var shopPreset = "33";
function shop_preset()
{
    action_zone.location ='http://'+IPandPort+'/'+'decoder_control.cgi?user='+user+'&pwd='+pwd+'&command='+shopPreset;
}

function postoffice_preset()
{
    action_zone.location ='http://'+IPandPort+'/'+'decoder_control.cgi?user='+user+'&pwd='+pwd+'&command='+poPreset;
}

</SCRIPT>

<input id="Post Office" type="button" value="Post Office" onclick="doFunction(postoffice_preset());" />
<input id="Shop" type="button" value="Shop" onclick="doFunction(shop_preset());" />

<IFRAME style="DISPLAY: none" src="" name=action_zone>
</IFRAME>

</body>
</html>

我想以最简单的方式实现的目的是: 周一至周五: 早上7点 - postoffice_preset() 下午6点 - shop_preset()

星期六: 早上7点 - postoffice_preset() 下午1点 - shop_preset()

孙: 没做什么。 ie:它保留在之前设置的shop_preset()上。

我不确定这是否完全有可能。这将在Internet Explorer上的最小化窗口上运行,该窗口永远不会与之交互。

2 个答案:

答案 0 :(得分:2)

您可以使用setInterval()来实现此目标。

window.setInterval()

那么你的代码可能就是这些问题:

var IPandPort = "ip:port";
var user = "user";
var pwd = "pass";
var poPreset = "31"; 
var shopPreset = "33";
var nIntervId;
// When the window is loaded, run the schedule.
window.onload = schedule();

function schedule() {
    // If there's a timer already running, clean it.
    if (typeof(nIntervId) != "undefined") {
        clearInterval(nIntervId);
    }

    // Run the positionCamera function every minute.
    nIntervId = setInterval(positionCamera, 60000);
}

function positionCamera() {
    var now = new Date();
    var weekDay = now.getDay();
    var hour = now.getHours();

    // Mon-Sat, 7am.
    if (weekDay >= 1 && weekDay <= 6 && hour == 7) {            
        postOfficeCamera();
    }
    // Mon-Fri, 6pm. Sat, 1pm. Sun, 24h.
    else if ((weekDay >= 1 && weekDay <= 5 && hour == 18) || 
             (weekDay == 7 && hour == 13) || 
             weekDay == 7) {            
        shopCamera();
    }    
}

function shopCamera() {
    action_zone.location ='http://'+IPandPort+'/'+'decoder_control.cgi?user='+user+'&pwd='+pwd+'&command='+shopPreset;
}

function postOfficeCamera() {
    action_zone.location ='http://'+IPandPort+'/'+'decoder_control.cgi?user='+user+'&pwd='+pwd+'&command='+poPreset;
}

Demo

答案 1 :(得分:0)

所以,你不会在这里找到任何能为你编码的人;那不是我们的工作。

我会创建各种规则引擎。在某个地方安排您的日程安排,定期检查您的日程安排并做些事情。

这样的事情:

(function() {
    var cameraInterval,
        zones = {
            'postoffice': 31,
            'shop': 33
        },
        zoneSchedule = {
            // Monday @ 7am
            107: zones.postoffice,
            // Monday @ 6pm
            118: zones.shop,

            // Tuesday @ 7am
            207: zones.postoffice,
            // Tuesday @ 6pm
            218: zones.shop,

            // Wednesday @ 7am
            307: zones.postoffice,
            // Wednesday @ 6pm
            318: zones.shop,

            // Thursday @ 7am
            407: zones.postoffice,
            // Thursday @ 6pm
            418: zones.shop,

            // Friday @ 7am
            507: zones.postoffice,
            // Friday @ 6pm
            518: zones.shop,

            // Saturday @ 7am
            607: zones.postoffice,
            // Saturday @ 1pm
            613: zones.shop
        },
        updateCamera = function() {
            var now = new Date(),
                nowIndex = now.getDay() + ('0' + now.getHours()).slice(-2),
                zoneCommand = zoneSchedule[nowIndex];

            // Only run this command if the current day/time is in the schedule
            if (zoneSchedule.hasOwnProperty(nowIndex)) {
                action_zone.location ='http://'+IPandPort+'/'+'decoder_control.cgi?user='+user+'&pwd='+pwd+'&command='+zoneCommand;
            }
        };

    // Check the schedule every hour
    cameraInterval = window.setInterval(updateCamera, 600000);
})();

注意:这不是开箱即用的。您需要对其进行自定义和调整,以使其适用于您的特定应用程序。

如果您决定试一试并遇到问题,请针对您的具体问题发布一个新问题。