javascript - 有没有办法只在事件发生后运行 p5.js?
问题描述
我只想在三个输入字段都不为空并且单击按钮后运行 p5.jsdraw()
和函数。setup()
function roundNumber(num, decimal_places) {
if (!("" + num).includes("e")) {
return +(Math.round(num + "e+" + decimal_places) + "e-" + decimal_places);
} else {
var arr = ("" + num).split("e");
var sig = ""
if (+arr[1] + scale > 0) {
sig = "+";
}
return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + decimal_places)) + "e-" + decimal_places);
}
} /* From https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary */
function computeSelection() {
const expr = document.forms[0].mathExpr.value;
var tempAns = parseFloat(eval(expr));
var roundedAnswer = roundNumber(tempAns, 10);
var answer = roundedAnswer;
document.getElementById("output").textContent = answer;
}
function get_x_min_x_max() {
const x_min = $("#x_min").value;
const x_max = $("#x_max").value;
var x_min_x_max = [x_min, x_max];
return x_min_x_max;
}
function setup() {
createCanvas(300, 300);
}
function draw() {
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<form>
<label>Graph y=</label>
<input id="mathExpr" type="text" name="mathExpr" value="">
<label> from x=</label>
<input id="x_min" type="text" name="x_min" value="">
<label> to </label>
<input id="x_max" type="text" name="x_max" value="">
<input type="button" name="result" value="Result" onclick="computeSelection();">
</form>
<h2>Answer: <span id="output"></span></h2>
</body>
如果这是我的代码,我只希望 js 代码底部的绘制和设置运行一次mathExpr
,x_min
并且x_max
都包含一些文本并单击结果按钮。我怎样才能做到这一点?
解决方案
推荐阅读
- css - 重构后样式组件停止工作
- julia - 朱莉娅:嵌套函数更慢吗?
- bash - 如何从 access.log 中提取不同的域
- c++ - 将 Windows 上的 GSL 与 CMake 链接的问题:错误 LNK2019:未解析的外部符号 gsl_root_fdfsolver_newton
- python - Pandas 交叉连接数据框和系列
- thunderbird - onNewMailReceived 事件返回包含上一个消息的消息列表
- python - 我不能从 Python 调用 Julia
- javascript - [href] 的错误重定向
- machine-learning - 3D CNN 预测质心
- python - 创建一个子程序来告诉玩家他们输入的坐标何时已经被占用