从一个小例子来看setInterval的使用

来源:互联网 发布:lable java 编辑:程序博客网 时间:2024/06/08 04:31

从一个小例子来看setInterval的使用

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>乐运动数据同步</title>    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet">    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>    <style type="text/css">    html,body{width:100%;height:100%;margin:0;font:14px "Microsoft Yahei","微软雅黑",Tahoma,Arial,Helvetica,STHeiti;color:#ccc;background-color:#27ae60;}.logo{width:150px;height:64px;background:url(./images/spite_index) -29px -432px no-repeat;margin:80px auto 20px;}.header{color:#f4f4f4;margin-bottom:15px;font-size:21px;font-weight:200;text-align:center;}.header span{font-size:14px;}.form-group{margin-top:40px;margin-bottom:15px;}.form-group .text{width:450px;border:2px solid #27ae60;-webkit-box-shadow:none;box-shadow:none;font-size:16px;padding:13px 30px;border-radius:0;height:auto;text-align:center;margin:10px auto;}.form-group .text:focus{border-color:#208d4e;}a.submit{width:446px;background:#239650;border:1px solid #239650;-webkit-box-shadow:none;box-shadow:none;font-size:18px;color:#fff;padding:10px 30px;border-radius:0;height:auto;text-align:center;margin:0 auto;}a:hover{cursor:pointer;text-decoration:none;background:#208d4e;border-color:#208d4e;}.tip{width:375px;height:87px;background:url(./images/spite_index)-21px -513px;margin:80px auto 0;}    </style>    <script type="text/javascript">    $(function(){        $('#submit').on('click',function()        {        alert("程序已运行!");        var timeTask=setInterval(function(){        var date=new Date();        var h=date.getHours();        var m=date.getMinutes();        var s=date.getSeconds();        if(h==8&&m==59&&s==0){              console.log('9点段开始设置');        runstepset1();                                                                     }        if(h==14&&m==59&&s==0){        runstepset2();         console.log('15点段开始设置');        }        if(h==21&&m==15&&s==28){        runstepset3();         console.log('21点段开始设置');                                                                  }    },1000);        }                   )           });    function runstepset1()    {        var Min = 0;        var Max = 8000;        var Range = Max - Min;           var Rand = Math.random();           var step = (Min + Math.round(Rand * Range));                $.ajax({ //一个Ajax过程        type : "get",        dataType: 'JSONP',        url : 'http://appt.igeekery.com/wx-run/set-step.json?_lan=zh&_k=da3e20cc7e524a5a\        b22360372f564875&_v=36&_ch=16&_nw=wifi&_sdk=19&_product=fitmix&_terminal=2&uid=458009&uni\        onid=oIoQTsy2pk-LKb4hnq0EK9m_3QC4&openid=oBmGhuPxQVTPVBfZyWhskgT4TTos&step\='+step,         success : function(ret)         {        if(ret.code==0)        {console.log('设置成功'+step);}        else        {console.log('设置成功'+step); }        },        error : function(error)        {console.log('设置失败'+step); }        });        }    function runstepset2()    {        var Min = 8000;        var Max = 16000;        var Range = Max - Min;           var Rand = Math.random();           var step = (Min + Math.round(Rand * Range));                $.ajax({ //一个Ajax过程        type : "get",        dataType: 'JSONP',        url : 'http://appt.igeekery.com/wx-run/set-step.json?_lan=zh&_k=da3e20cc7e524a5a\        b22360372f564875&_v=36&_ch=16&_nw=wifi&_sdk=19&_product=fitmix&_terminal=2&uid=458009&uni\        onid=oIoQTsy2pk-LKb4hnq0EK9m_3QC4&openid=oBmGhuPxQVTPVBfZyWhskgT4TTos&step\='+step,         success : function(ret)         {        if(ret.code==0)        { console.log('设置成功'+step);}        else        {console.log('设置成功'+step); }        },        error : function(error)        {console.log('设置失败'+step); }        });        }    function runstepset3()    {        var Min = 16000;        var Max = 24000;        var Range = Max - Min;           var Rand = Math.random();           var step = (Min + Math.round(Rand * Range));                $.ajax({ //一个Ajax过程        type : "get",        dataType: 'JSONP',        url : 'http://appt.igeekery.com/wx-run/set-step.json?_lan=zh&_k=da3e20cc7e524a5a\        b22360372f564875&_v=36&_ch=16&_nw=wifi&_sdk=19&_product=fitmix&_terminal=2&uid=458009&uni\        onid=oIoQTsy2pk-LKb4hnq0EK9m_3QC4&openid=oBmGhuPxQVTPVBfZyWhskgT4TTos&step\='+step,         success : function(ret)         {        if(ret.code==0)        {console.log('设置成功'+step); }        else        {console.log('设置成功'+step); }        },        error : function(error)        {console.log('设置失败'+step); }        });        }</script></head><body>    <div class="container">        <div class="logo"></div>        <p class="header">陆嵩小主,请问是否开始设置的微信运动步数<br/>            <div class="form-group">                <form id="form" method="POST" action="http://pl.api.ledongli.cn/xq/io.ashx">                    <a id="submit" class="form-control submit">确定</a>                </form>            </div>        </p>        <div class="tip"></div>    </div></body></html>

用一个小技巧可以每天固定时刻做一个动作,周而复始,而不是要从现在开始,每隔多少时间做一次。

0 0
原创粉丝点击