简体   繁体   English

Javascript 倒计时应用程序不起作用

[英]Javascript Countdown application doesn't work

I'm trying to create a simple counter, so that every request made to path \count would decrease the count by one.我正在尝试创建一个简单的计数器,以便对路径 \count 发出的每个请求都会将计数减一。 Once it's below 1, it will response with the message "Finished".一旦它低于 1,它将响应消息“完成”。 Requests to any other path would get a response "None".对任何其他路径的请求都会得到响应“无”。 Can someone tell me what to change?有人可以告诉我要改变什么吗?

let count = 5;
const handleRequest = (request) => {  
  const url = new URL(request.url);
 
  let message = "Finished";   
  if (url.pathname === "/count" && count >=1) {


count--;
    return new Response(count); 
     
  } else if (url.pathname ==="/count" && count < 1); { 
    return new Response(message); 
  } else {
    return new Response("None") 
  } 
};

By the looks of it, it appears that you are returning a value before decrementing the count.从外观上看,您似乎在减少计数之前返回了一个值。 This stops the decrementing line from running and therefore not affecting the count.这会阻止递减线运行,因此不会影响计数。

Try swapping the return statement and the decrement:尝试交换 return 语句和减量:

let count = 5;
...
if (url.pathname === "/count" && count >=1) { 
    count--; 
    return new Response(count); 
} else if (url.pathname ==="/count" && count < 1); { 
 ...
} 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM