I want to read 2 inputs from user and save it from my program. When the user types his input value and submits, the function getValue will be called and values will be put into duration and interval. But when initBuffer is called after getValue, it always tell me that duration and interval are still null. I thought they are global variables...
Can somebody please tell me where I am wrong? Thanks a lot.
//main.js
var duration = null;
var interval = null;
function getValue(){
interval = parseInt(document.getElementById('interval').value);
duration = parseInt(document.getElementById('duration').value);
console.log(window.interval);
if(isNaN(window.interval) || isNaN(window.duration) ){
alert("please give 2 values at the same time");
}
console.log("duration: "+ window.duration + "interval:" + window.interval);
}
function start(){
initBuffer();
}
function initBuffer(){
alert(window.duration);
if(window.duration == null){
console.log('duration set to default: 40');
window.duration = 40; //default (min)
}
if(window.interval == null){
console.log('interval set to default: 5');
window.interval = 5; //default
}
numSamples= Math.floor(duration* 60 /interval);
}
<html>
<head>
<meta charset="utf-8">
<title>Graph</title>
</head>
<style>
#start {
position:absolute;
left:1080px;
top: 30px;
width:30px;
height:30px;
background: #2280ff;
font-size: 12px;
color: white;
border-radius: 10px;
padding: 1em;
}
.div-inline{
display: inline
}
</style>
<body>
<form id = "hint">
<div class = "div-inline">
Interval:
<input type="text" name="interval" id = "interval">(s)
</div>
<div class = "div-inline">
Duration:
<input type = "text" name = "duration" id = "duration">(min)
<input type = "submit" value = "submit" onclick = "getValue() return false">
</div>
<br>
(Default: Interval = 5s, Duration = 40 min)
</form>
<script type="text/javascript">
</script>
<div id = "sec1">
<canvas id = "display" width = "1024" height = "300"></canvas>
</div>
<div id = "start" onclick = "start()" >start</div>
<script src = "main.js" charset="utf-8"></script>
</body>
</html>