【问题标题】:JS/JQuery/Arduino update div automaticallyJS/JQuery/Arduino 自动更新 div
【发布时间】:2015-09-08 20:57:00
【问题描述】:

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

此时,服务器仅向 Web 客户端打印一条语句,“低”、“中”或“高”取决于传感器的状态。

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

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

  <!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 打印状态和脚本来检测它并自动更新它的最佳方法是什么?

【问题讨论】:

    标签: javascript jquery arduino


    【解决方案1】:

    只需执行以下操作即可使用 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> 
    

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2010-10-05
      • 1970-01-01
      • 2019-12-12
      相关资源
      最近更新 更多