每次JavaScript增量数(if语句)为true
在我的代码中,我使用AJAX每30秒从数据库中检索一次数据。我想使用JavaScript在每次从数据库接收数据(每30秒一次)以及if语句为true时递增变量wht。下面的代码正在工作并递增到1,但不会超过1。有人能解决这个问题吗每次JavaScript增量数(if语句)为true,javascript,Javascript,在我的代码中,我使用AJAX每30秒从数据库中检索一次数据。我想使用JavaScript在每次从数据库接收数据(每30秒一次)以及if语句为true时递增变量wht。下面的代码正在工作并递增到1,但不会超过1。有人能解决这个问题吗 <script> $(document).ready(function () { ajax_call = function() { $.ajax
<script>
$(document).ready(function () {
ajax_call = function() {
$.ajax({
type: "GET",
url: "test.php",
dataType: "html",
success: function (response) {
color = response;
console.log(color);
if (color == white){
var wht = (function(w) {
return function() {
w += 1;
return w;
}
}(0));
document.getElementById("memo").value = wht();
}else{
console.log("Color is not white");
}
var interval = 30000;
setInterval(ajax_call, interval);
});
</script>
<script>
const minusButtonFw = document.getElementById('memo-minus');
const plusButtonFw = document.getElementById('memo-plus');
var memo = document.getElementById('memo');
minusButtonFw.addEventListener('click', event => {
event.preventDefault();
const currentValue = Number(memo.value);
memo.value = currentValue - 1;
});
plusButtonFw.addEventListener('click', event => {
event.preventDefault();
const currentValue = Number(memo.value);
memo.value = currentValue + 1;
});
</script>
$(文档).ready(函数(){
ajax\u call=function(){
$.ajax({
键入:“获取”,
url:“test.php”,
数据类型:“html”,
成功:功能(响应){
颜色=反应;
控制台。日志(颜色);
如果(颜色==白色){
var wht=(函数(w){
返回函数(){
w+=1;
返回w;
}
}(0));
document.getElementById(“备忘录”).value=wht();
}否则{
console.log(“颜色不是白色”);
}
var区间=30000;
setInterval(ajax_调用,interval);
});
const minusButtonFw=document.getElementById('memo-减号');
const plusButtonFw=document.getElementById('memo-plus');
var memo=document.getElementById('memo');
minusButtonFw.addEventListener('单击',事件=>{
event.preventDefault();
const currentValue=编号(备注值);
memo.value=当前值-1;
});
plusButtonFw.addEventListener('单击',事件=>{
event.preventDefault();
const currentValue=编号(备注值);
memo.value=当前值+1;
});
我用setInterval
做了一个例子。我把w
设为全局,这样它就可以工作了。试试这个:
var w=0;
var interval=setInterval(函数(){
如果(颜色==白色){
w++;
document.getElementById(“备忘录”).value=w;
}否则{
console.log(“颜色不是白色”);
}
}, 30000);
您需要跟踪“w”。您当前的设置将“w”用作函数的参数。您需要将其保留在函数外部,并从函数内部递增。您还需要将该函数以类似以下的间隔包装:
var w = 0;
function setWhite(color) {
if (color == white) {
w++;
document.getElementById("memo").value = w;
} else {
console.log("Color is not white");
}
}
setInterval(function() {
setWhite(color);
}, 30000);
这应该可以满足您的需要。我没有运行代码,因此可能存在需要更正的语法错误。请尝试更改行
document.getElementById("memo").value = wht();
到
您的完整代码:
<script>
$(document).ready(function () {
ajax_call = function() {
$.ajax({
type: "GET",
url: "test.php",
dataType: "html",
success: function (response) {
color = response;
console.log(color);
if (color == white){
var wht = (function(w) {
return function() {
w += 1;
return w;
}
}(0));
document.getElementById("memo").value = wht(document.getElementById("memo").value);
}else{
console.log("Color is not white");
}
var interval = 30000;
setInterval(ajax_call, interval);
});
</script>
$(文档).ready(函数(){
ajax\u call=function(){
$.ajax({
键入:“获取”,
url:“test.php”,
数据类型:“html”,
成功:功能(响应){
颜色=反应;
控制台。日志(颜色);
如果(颜色==白色){
var wht=(函数(w){
返回函数(){
w+=1;
返回w;
}
}(0));
document.getElementById(“备忘录”).value=wht(document.getElementById(“备忘录”).value);
}否则{
console.log(“颜色不是白色”);
}
var区间=30000;
setInterval(ajax_调用,interval);
});
首先,变量wht
是一个函数。如果您只想跟踪If条件为真的时间,可以通过使变量静态(文字)来实现。您可以通过将变量存储在全局范围内来实现这一点
在定义wht的代码中也存在sytax错误
试试这个
$(function () {
var memo = document.getElementById("memo");
memo.val = 0;
var ajax_call = function () {
$.ajax({
type: "GET",
url: "test.php",
dataType: "html",
success: function (response) {
color = response;
console.log(color);
if (color == white) {
memo.val++;
memo.value = memo.val;
} else {
console.log("Color is not white");
}
}
});
}
var interval = 30000;
setInterval(ajax_call, interval);
});
注意事项:
如果响应由您管理,我建议您将响应作为json发送,而不是简单地将其作为只包含一种值颜色的html发送。您是否在寻找
setInterval
?您必须提供更多信息。@Naren如果语句为true,我希望每次都增加变量wht。您一直将0传递给wht
函数n、 我想你可能是想传递document.getElementById(“备忘录”).value
?@crayron:你完全正确。我不知道如何编写我的函数,使它添加到最后一个递增的数字。我的错。我一定花了太长时间提交我的答案,你已经回答了。现在有一个副本。我可以删除它吗?谢谢你的答案。如果我不使用setInterva,你的代码会工作吗我是其中的一部分?因为我已经在AJAX中使用了setInterval。@CoderJay,如果已经有一个interval,只需在interval中包含函数,并使w
成为一个全局变量。@MyJobistBehappy我会的,谢谢。谢谢兄弟,你的代码运行得很好。jazakallah。@CoderJay NM&Ameen。别忘了设置答案:)。
$(function () {
var memo = document.getElementById("memo");
memo.val = 0;
var ajax_call = function () {
$.ajax({
type: "GET",
url: "test.php",
dataType: "html",
success: function (response) {
color = response;
console.log(color);
if (color == white) {
memo.val++;
memo.value = memo.val;
} else {
console.log("Color is not white");
}
}
});
}
var interval = 30000;
setInterval(ajax_call, interval);
});