settimeout()函数在函数内部不起作用



所以,这里是函数,当我把setTimeout()函数在checkForm函数不起作用时,也试着把它放在checkForm函数之外,它也不起作用。

function checkForm(event) {
event.preventDefault();
var name = document.getElementById('username').value;
var email = document.getElementById('user-email').value;
var message = document.getElementById('message').value;
var error = "";
if (name.length == 1 || email.length == 1 || message.length == 1) {
error = "Min length of the fields is 2 character."
} else if (name == "" || email == "" || message == "") {
error = "Fields are empty."
} else if (name.length > 15 || email.length > 20 || message.length > 100) {
error = "Max length of the fields is 15 charcaters."
}
if (error == "") {
alert("You sucessfully filled up the form.");
setTimeout(function () {
window.location = "https://www.ebay.com/"
}, 5000);
} else {
document.getElementById('errorDiv').innerHTML = error;
}
}

您需要更改为window.location.href = "your link"

function checkForm(event) {

setTimeout(function () {
window.location.href = "https://www.ebay.com/";
alert('go to ebay');
}, 2000);
}
<button onclick="checkForm()">Check Form</button>

你试试这个

setTimeout(function () {
location.href = "https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_href_set";
}, 2000);

有可能弹出警告,而您没有确认弹出窗口,这会导致浏览器等待您确认弹出窗口。如果是这种情况,请删除警告("您已成功填写了表单。")并检查它是否有效

最新更新