Home  >  Article  >  Backend Development  >  How to automatically close online answering questions at a scheduled time

How to automatically close online answering questions at a scheduled time

PHPz
PHPzOriginal
2023-09-24 17:45:11866browse

How to automatically close online answering questions at a scheduled time

How to automatically close online answering questions at a scheduled time

Currently, with the development of the Internet, various online answering platforms have become important for students to learn independently and improve their abilities. tool. Some online answering platforms may provide a scheduled shutdown function, which automatically closes answering after the specified time. This function can prevent students from continuing to participate in answering questions after the answering time is over, ensuring fairness and justice. This article will introduce how to use specific code examples to automatically close online answering questions at a scheduled time.

To realize the scheduled automatic shutdown of online answering questions, you first need to clarify the technology stack used. Generally speaking, we can use front-end HTML, CSS and JavaScript technologies to complete page design and interaction, and the back-end can choose to use a server-side language, such as Node.js, PHP, etc. In this example, we will use HTML, CSS, and JavaScript to implement the front end, and Node.js as the backend.

The following is a possible implementation:

  1. Create the HTML page structure

First, we need to create the structure of the answer page in HTML. You can use form elements to accept answer submissions from students, and set up a countdown area on the page to display in real time how much time is left until the end of the answer. The sample code is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>在线答题</title>
</head>
<body>
  <h1>在线答题</h1>
  <form id="answerForm" action="submit.php" method="POST">
    <!-- 答题内容 -->
    <!-- 提交按钮 -->
  </form>
  <div id="countdown">距离答题结束还剩:</div>
</body>
</html>
  1. Write JavaScript code to implement the countdown function

In the countdown area of ​​HTML, we can use JavaScript code to implement the timer function. The sample code is as follows:

var countdown = document.getElementById('countdown');
var endTime = new Date('2021/01/01 00:00:00');  // 答题结束时间
var timer = setInterval(function() {
  var nowTime = new Date();
  var timeLeft = Math.floor((endTime - nowTime) / 1000);  // 计算剩余时间(秒)
  var hours = Math.floor(timeLeft / 3600);
  var minutes = Math.floor((timeLeft - hours * 3600) / 60);
  var seconds = timeLeft - hours * 3600 - minutes * 60;
  countdown.innerHTML = '距离答题结束还剩:' + hours + '小时' + minutes + '分钟' + seconds + '秒';
  
  if (timeLeft <= 0) {
    clearInterval(timer);
    alert('答题时间已结束,系统将自动关闭答题。');
    // 在此处添加页面自动跳转或关闭答题的逻辑
  }
}, 1000);

In the above code, we first obtain the elements of the countdown area through the getElementById method, then set the end time of the answer, use the setInterval method to update the remaining time every second, and based on the remaining time Different situations are used to determine whether the answering time has exceeded.

  1. Write back-end code to verify the answering time

In the backend, we need to verify whether the answering time has ended. This can be achieved by verifying the relationship between the current time and the end time of the answer in the interface for submitting answers. The sample code is as follows:

app.post('/submit', function(req, res) {
  var nowTime = Date.now();
  var endTime = new Date('2021/01/01 00:00:00');  // 答题结束时间
  if (nowTime > endTime) {
    res.send('答题时间已结束,无法提交答案。');
  } else {
    // 处理答案提交逻辑
  }
});

In this code, we first obtain the current time through the Date.now() method, and then compare it with the end time of the answer. If the current time has exceeded the answering end time, a prompt message that the answering time has ended will be returned; otherwise, the answer submission logic can continue to be processed.

It should be noted that the above is just a simple example, and the actual question answering platform may have more functional requirements. In practical applications, we also need to do a good job in security verification, database operation and data processing.

Through the above examples, we can see how to automatically close online answering questions at a scheduled time through specific codes. Such a function can ensure the fairness and order of answering questions, and improve students' learning effect and learning experience.

The above is the detailed content of How to automatically close online answering questions at a scheduled time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn