Вот несколько способов создания двухчасового таймера с использованием примеров кода на разных языках программирования:
-
Python:
import time def two_hour_timer(): start_time = time.time() end_time = start_time + 2 * 60 * 60 # 2 hours = 2 * 60 minutes * 60 seconds while time.time() < end_time: # Do something during the two-hour period pass print("Two-hour timer has ended!") two_hour_timer()
-
JavaScript:
function twoHourTimer() { var startTime = new Date().getTime(); var endTime = startTime + 2 * 60 * 60 * 1000; // 2 hours = 2 * 60 minutes * 60 seconds * 1000 milliseconds while (new Date().getTime() < endTime) { // Do something during the two-hour period } console.log("Two-hour timer has ended!"); } twoHourTimer();
-
Java:
public class TwoHourTimer { public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); long endTime = startTime + 2 * 60 * 60 * 1000; // 2 hours = 2 * 60 minutes * 60 seconds * 1000 milliseconds while (System.currentTimeMillis() < endTime) { // Do something during the two-hour period } System.out.println("Two-hour timer has ended!"); } }
-
C#:
using System; public class TwoHourTimer { public static void Main(string[] args) { DateTime startTime = DateTime.Now; DateTime endTime = startTime.AddHours(2); while (DateTime.Now < endTime) { // Do something during the two-hour period } Console.WriteLine("Two-hour timer has ended!"); } }