为了账号安全,请及时绑定邮箱和手机立即绑定

使用ajax和setTimeout()发送两个布尔值的问题

使用ajax和setTimeout()发送两个布尔值的问题

摇曳的蔷薇 2023-12-14 16:11:00
我有两个函数,每个函数应该使用 ajax 在 php 页面上发送 0 或 1。当按下键盘上的某个键时,发送 1 的函数将启动,而发送 0 的函数必须在三秒后通过 setTimeout() 启动。问题是第二个函数不发送。我把相应的代码发给你当事人。预先感谢您的帮助,请原谅我的英语读起来不太好^^'我的代码:function typing() {  var typingBool = 1  if (typingBool != "") {    let donneee = {}    donneee["typingBool"] = typingBool    let donneeeJson = JSON.stringify(donneee)    let xmlhttp = new XMLHttpRequest()    xmlhttp.open("POST", "ajax/typing.php")    xmlhttp.send(donneeeJson)  }}function typing2() {  var typingBool = 0  if (typingBool != "") {    let donneee = {}    donneee["typingBool"] = typingBool    let donneeeJson = JSON.stringify(donneee)    let xmlhttp = new XMLHttpRequest()    xmlhttp.open("POST", "ajax/typing.php")    xmlhttp.send(donneeeJson)  }}var typingText = document.getElementById('texte');typingText.addEventListener('keypress', function() {  typing();  setTimeout(function() {    typing2()  }, 3000);})
查看完整描述

1 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

这里的主要问题是&之间的区别!=!==

if (0 !=  "") { console.log("Won't be displayed"); }
if (0 !== "") { console.log("Will be displayed");  }

话虽这么说,您的代码可以缩短一点,这应该可以工作:


function typing(typingBool){

  let donneeeJson = JSON.stringify({ typingBool: typingBool });

  let xmlhttp = new XMLHttpRequest();

     

  xmlhttp.open("POST", "ajax/typing.php");

  xmlhttp.send(donneeeJson);

}


var typingText = document.getElementById('texte');

typingText.addEventListener('keypress', function(){

  typing(1);

  setTimeout(function(){ typing(0); }, 3000);

});


查看完整回答
反对 回复 2023-12-14
  • 1 回答
  • 0 关注
  • 42 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信