Методы создания 2-часового таймера с примерами кода на Python, JavaScript, Java и C#

Вот несколько способов создания двухчасового таймера с использованием примеров кода на разных языках программирования:

  1. 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()
  2. 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();
  3. 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!");
    }
    }
  4. 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!");
    }
    }