Javascript 周计划函数

标签 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()

太阳: 没做什么。即:它保留在之前设置的shop_preset()上。

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

最佳答案

您可以使用 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

关于Javascript 周计划函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955222/

相关文章:

function - 如何在 Scala 中优化这个短阶乘函数? (创建 50000 个 BigInt)

javascript - 使用 AJAX 更新从数据库(mysql)检索的数据

javascript - 在javascript中为表保留空间

javascript - 如何在特定数字的每两个偶数位之间插入破折号

javascript - Jquery ui 可调整高度 - 默认高度 :0px

php - 图片上传脚本在实时服务器中不起作用,但在本地主机中起作用

string - Instr 函数无法检测到字符串中的单词

javascript - 我可以将助手传递给 Handlebars 函数吗?

来自触发它的同一 Activity 的 Android 句柄/覆盖/中断 Intent

c++ - 在不提供参数类型的情况下绑定(bind)函数