自动刷新在javascript中不起作用
我尝试使用自动刷新在javascript中不起作用,javascript,jquery,setinterval,Javascript,Jquery,Setinterval,我尝试使用jQuery和circularchart绘制圆形量规,我能够完成 我希望javascript的setInterval()函数能够自动刷新值,以便仪表值能够自动更新,而无需手动刷新 但是setinterval()函数根本不起作用 我不想刷新整个页面或html的主体 我只想刷新特定的circleChart#0函数 需要你的帮助 这是circle.html 假设{{temperature}}是某种模板变量,那么从模板生成页面时,这些变量(通常)只计算一次 您将需要某种AJAX调用来获取更新
jQuery
和circularchart
绘制圆形量规,我能够完成
我希望javascript的setInterval()
函数能够自动刷新值,以便仪表值能够自动更新,而无需手动刷新
但是setinterval()
函数根本不起作用
我不想刷新整个页面或html的主体
我只想刷新特定的circleChart#0
函数
需要你的帮助
这是circle.html
假设
{{temperature}}
是某种模板变量,那么从模板生成页面时,这些变量(通常)只计算一次
您将需要某种AJAX调用来获取更新的温度值。假设
{{temperature}
是某种模板变量,这些变量(通常)在从模板生成页面时只计算一次
您将需要某种AJAX调用来获取更新的温度值。@Milancheda您确定没有从代码中编辑出一些模板标记或可能的错误源吗?是@Teemu Nothing removed。刚刚格式化。我有温度传感器连接到我的树莓皮。因此,当我把传感器放在热水和冰水中时,我测试我网站上的圆形仪表值是否更新。并且仪表值仅在我手动刷新页面时更新@JamesThorpe@MilanChheda
{{temperature}}
是一个模板变量,您在“格式化”中破坏了该变量。您在哪里定义了temperature
?@MilanChheda您确定没有从代码中编辑出一些模板标记,或者可能的错误源?是@Teemu未删除任何内容。刚刚格式化。我有温度传感器连接到我的树莓皮。因此,当我把传感器放在热水和冰水中时,我测试我网站上的圆形仪表值是否更新。并且仪表值仅在我手动刷新页面时更新@JamesThorpe@MilanChheda{{temperature}}
是一个模板变量,你用“格式化”破坏了它。你在哪里定义了温度
?对不起,兄弟,但我不明白。但是当我手动刷新页面时,为什么templates变量值是update?所以setInterval()函数不能与模板变量一起工作,是吗?对不起,兄弟,但我无法理解。但是当我手动刷新页面时,为什么templates变量值是update?所以setInterval()函数不能与模板变量一起工作,是吗?
<body>
<div class="circleChart" id="0"></div>
<div class="circleChart" id="1" data-value="77"></div>
<script src="{{ url_for('static', filename='jquery-1.12.4.min.js') }}"></script>
<script src="{{ url_for('static', filename='circleChart.js') }}"></script>
<script>
$(".circleChart#1").circleChart();
$(".circleChart#0").circleChart({
size: 200,
value: {{temperature}},
text: 0,
onDraw: function(el, circle) {
circle.text(Math.round(circle.value) + "'C");
}
});
setInterval(function() {
$("#0").circleChart({
value: {{temperature}},
onDraw: function(el, circle) {
circle.text(Math.round(circle.value) + "'C");
}
});
}, 2000);
</script>
</body>
</html>
#!/usr/bin/python
from flask import Flask, render_template
app = Flask(__name__)
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
@app.route("/")
def main():
temperature , humidity = read_temp()
templateData = {
'temperature' : temperature,
'humidity': humidity
}
return render_template('circle.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)