有没有办法在表单中的 onsubmit 操作之前暂停或等待函数完成
Is there a way to pause or wait for a function to finish before onsubmit action in form
我有一个简单的例子,我发现 onsubmit 立即需要一个 return 值。我有一个暂停 5 秒并等到它完成然后 return 返回布尔值以模拟其他 javascript 处理操作的函数。但是,即使在我的函数设法 return 任何值之前,页面表单提交仍然会触发。那么我应该怎么做,在 JS 中使用回调对我的情况有帮助吗?
<html>
<body>
<form action="test.html" onsubmit="return testing();">
<button type="submit"> hello world </button>
</form>
</body>
<script>
function testing()
{
var newState = -1;
setTimeout(function() {
if (newState == -1) {
alert('VIDEO HAS STOPPED');
output = "newText";
console.log("AFTER 5 Sec: " + output);
return false;
}
}, 5000);
//return false;
}
</script>
</html>
一种选择是在按钮点击本身中调用 testing()。您可以在超时函数内手动调用 submit()。但是调用testing()后一定要return false。
例如:
<form id="myForm" action="test.html">
<button type="submit" onclick="testing(); return false;"> hello world </button>
</form>
<script type="javascript">
function testing()
{
var newState = -1;
setTimeout(function() {
if (newState == -1) {
alert('VIDEO HAS STOPPED');
output = "newText";
console.log("AFTER 5 Sec: " + output);
document.getElementById("myForm").submit();
return false;
}
}, 5000);
//return false;
}
</script>
顺便说一句,似乎不需要 newState 变量。
表单 不能 提交,除非 'function finishes' 提交。
超时将调用一个不同的函数,该函数在未来的某个时刻独立运行;只需禁用基于进程的 flag/state 的默认提交操作。
我把这些功能分开了,这样更清楚。
var state; // undefined, 'waiting', 'done'
function waitForVideo () {
alert('VIDEO HAS STOPPED');
console.log("AFTER 5 Sec: " + output);
// call submit again (or do whatever)
state = 'done';
document.forms[0].submit();
}
function testing()
{
if (!state)
{
state = 'waiting';
// function queued for timeout - not run immediately
setTimeout(waitForVideo, 5000);
}
if (state === 'waiting') {
// don't submit - timeout not done
return false;
}
}
我有一个简单的例子,我发现 onsubmit 立即需要一个 return 值。我有一个暂停 5 秒并等到它完成然后 return 返回布尔值以模拟其他 javascript 处理操作的函数。但是,即使在我的函数设法 return 任何值之前,页面表单提交仍然会触发。那么我应该怎么做,在 JS 中使用回调对我的情况有帮助吗?
<html>
<body>
<form action="test.html" onsubmit="return testing();">
<button type="submit"> hello world </button>
</form>
</body>
<script>
function testing()
{
var newState = -1;
setTimeout(function() {
if (newState == -1) {
alert('VIDEO HAS STOPPED');
output = "newText";
console.log("AFTER 5 Sec: " + output);
return false;
}
}, 5000);
//return false;
}
</script>
</html>
一种选择是在按钮点击本身中调用 testing()。您可以在超时函数内手动调用 submit()。但是调用testing()后一定要return false。
例如:
<form id="myForm" action="test.html">
<button type="submit" onclick="testing(); return false;"> hello world </button>
</form>
<script type="javascript">
function testing()
{
var newState = -1;
setTimeout(function() {
if (newState == -1) {
alert('VIDEO HAS STOPPED');
output = "newText";
console.log("AFTER 5 Sec: " + output);
document.getElementById("myForm").submit();
return false;
}
}, 5000);
//return false;
}
</script>
顺便说一句,似乎不需要 newState 变量。
表单 不能 提交,除非 'function finishes' 提交。
超时将调用一个不同的函数,该函数在未来的某个时刻独立运行;只需禁用基于进程的 flag/state 的默认提交操作。
我把这些功能分开了,这样更清楚。
var state; // undefined, 'waiting', 'done'
function waitForVideo () {
alert('VIDEO HAS STOPPED');
console.log("AFTER 5 Sec: " + output);
// call submit again (or do whatever)
state = 'done';
document.forms[0].submit();
}
function testing()
{
if (!state)
{
state = 'waiting';
// function queued for timeout - not run immediately
setTimeout(waitForVideo, 5000);
}
if (state === 'waiting') {
// don't submit - timeout not done
return false;
}
}