[英]how to get value from other script? (in html)
I want to get value from other script. 我想从其他脚本中获取价值。
(about socket.io) A script -> (about highchart) B script (关于socket.io)A脚本->(关于highchart) B脚本
this is A script 这是一个脚本
<script>
var socket = io();
socket.on('currTemp', function(data){
document.getElementById("temp").innerHTML = "Temperature : " + data;
***// I WANT 'data'.***
}); </script>
and this is B script 这是B脚本
<script type="text/javascript">
....
code
....
setInterval(function () {
var point,
newVal,
inc;
if (chartRpm) {
point = chartRpm.series[0].points[0];
inc = ------> ***I want 'inc = data '***
newVal = inc;
if (newVal < 0 || newVal > 1200) {
newVal = point.y - inc;
}
point.update(newVal);
}}, 2000); </script>
this code is in same file. 此代码在同一文件中。 how to do that ? 怎么做 ?
You can assign a global variable in one script like this: 您可以在一个脚本中分配一个全局变量,如下所示:
window.myVariable = "some_value";
and then access it like you would any other variable in your other script; 然后像访问其他脚本中的其他变量一样访问它;
Declare a var globally simply with var data;
仅使用var data;
在全局上声明var data;
Both scripts should be able to access the variable. 这两个脚本都应该能够访问该变量。
Do check out https://www.w3schools.com/js/js_scope.asp . 请检查https://www.w3schools.com/js/js_scope.asp 。
You options are: 您可以选择的是:
1- Global Variable: By Assigning window.data = data in A script and use it in B script by using inc = window.data 1-全局变量:通过在A脚本中分配window.data =数据 ,并通过使用inc = window.data在B脚本中使用它
2- Use global events where A script fires an event with data attached to it and it will caught by B script. 2-使用全局事件,其中A脚本会触发事件并附加数据,该事件将被B脚本捕获。
3- Use any JS state management lib to share data between "components" 3-使用任何JS状态管理库在“组件”之间共享数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.