ajax同步请求指定时间超时

主要使用await,async,Promise实现

<!DOCTYPE html>
<html>
	<head>
		<meta charsset="utf-8" />
		<title></title>
	</head>
	<body>
		<input type="button" onclick="e()" value="这是一个按钮" />
	</body>
	<script src="js/jquery-3.6.0.min.js"></script>
	<script>
		async function e() {
			console.log("kaishi1");
			let result = await a();
			console.log("jieshu==========>" + result);
		}
		async function a() {
			return await b();
		}
		function b() {

			if (1 != 1) {
				return lightId;
			} else {
				return new Promise(function(resolve, reject) {
					$.ajax("http://192.168.1.210/state", {
						method: "put",
						dataType: "json",
						timeout: 3000,
						success: function(res) {
							resolve(true);
						},
						error: function() {
							console.log("失败");
							resolve(false);
						}
					});
				});
			}
		}
	</script>
</html>

作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文地址:https://www.cnblogs.com/JaminYe/p/15548502.html