JS / JQuery / Arduino自动更新div

时间:2015-09-08 20:57:00

标签: javascript jquery arduino

我创建了一个Arduino项目,该项目目前正在托管一个每10秒刷新一次的本地服务器(元内容)。

此时服务器只是根据传感器的状态向网络客户端打印一个语句,“低”,“中”或“高”。

我尝试使用css / js创建一个脉冲圆来显示温度(蓝色表示低,橙色表示中等,红色表示高)。我想做的是让圆圈根据传感器的状态自动改变颜色,而无需元内容刷新或按回车键。

到目前为止,我一直在尝试以下方法:

  <!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Alert</title>
<!-- css -->
<style>
#phone {
    z-index: -1;
    position:relative;
}
.holder {
    margin-top: 20px;
    margin-left: 30px;
    position: relative;
}
.circle {
    width: 160px;
    height: 160px;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
    background-color: #eb6d05;
    z-index: 10;
    position: absolute;
    top: 40px;
    left: 65px;
}
.ring {
    border-width: 10px;
    border-style: solid;
    background: transparent;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
    height: 200px;
    width: 200px;
    -webkit-animation: pulse 2.5s ease-out;
    -moz-animation: pulse 2.5s ease-out;
    animation: pulse 2.5s ease-out;
    -webkit-animation-iteration-count: infinite;
    -moz-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    position: absolute;
    top: 11px;
    left: 35px;
    z-index: 1;
    opacity: 0;
}
#holder .default .circle {
    background-color: #eb6d05;
}
#holder .default .ring {
        border-color: #eb6d05;

}
#holder .sync .circle {
    background-color: #57C1C1;
}
#holder .sync .ring {
        border-color: #57C1C1;

}
#holder .alert .circle {
    background-color: #E4332A;
}
#holder .alert .ring {
        border-color: #E4332A;

}

 @-moz-keyframes pulse {
 0% {
 -moz-transform: scale(0);
 opacity: 0.0;
}
 25% {
 -moz-transform: scale(0);
 opacity: 0.1;
}
 50% {
 -moz-transform: scale(0.1);
 opacity: 0.3;
}
 75% {
 -moz-transform: scale(0.5);
 opacity: 0.5;
}
 100% {
 -moz-transform: scale(1);
 opacity: 0.0;
}
}
 @-webkit-keyframes "pulse" {
 0% {
 -webkit-transform: scale(0);
 opacity: 0.0;
}
 25% {
 -webkit-transform: scale(0);
 opacity: 0.1;
}
 50% {
 -webkit-transform: scale(0.1);
 opacity: 0.3;
}
 75% {
 -webkit-transform: scale(0.5);
 opacity: 0.5;
}
 100% {
 -webkit-transform: scale(1);
 opacity: 0.0;
}
}
</style>
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-custom">
<div id="holder">
  <div id="phone" class="default"><!--<img src="" border="0" height="564px">-->
    <div class="ring"></div>
    <div class="circle"></div>
  </div>
</div>
<div>
  <input name="" id="txt" type="text" value="">
</div>
<!-- Core JavaScript Files   --> <script src="js/jquery.min.js"></script> 
<script type="text/javascript">
    function notify() {
        var ct = $( "#txt" ).val();
        console.log(ct);

    if(ct == "a") {
        $("#phone").removeClass();
        $("#phone").addClass("alert");
        console.log("1");
    }
    else if(ct == "b") {
        $("#phone").removeClass();
        $("#phone").addClass("sync");
        console.log("2");
    }
    else {
        $("#phone").removeClass();
        $("#phone").addClass("default");
        console.log("3");
    }
}
    $( "#txt" ).on( "change", notify );
    </script> 

</body>
</html>

让Arduino打印状态和脚本检测并自动更新的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

只需执行以下操作即可获取ajax的服务器答案:

<script type="text/javascript">
    function notify() {
        var ct = $( "#txt" ).val();
        console.log(ct);

    if(ct == "a") {
        $("#phone").removeClass();
        $("#phone").addClass("alert");
        console.log("1");
    }
    else if(ct == "b") {
        $("#phone").removeClass();
        $("#phone").addClass("sync");
        console.log("2");
    }
    else {
        $("#phone").removeClass();
        $("#phone").addClass("default");
        console.log("3");
    }
}
    $( "#txt" ).on( "change", notify );

$.ajax({
     url: 'serverurl',
     dataType: 'text'
}).done(function(data){
     $("#txt").val(data);
});
</script>