MohsankhanDEV
  • Home
  • Make Money
  • _Forex
  • Programming
  • _Html
  • _Css
  • _Javascript
  • Food
  • Life
  • Health
  • Finance
  • Android
Home

casino logic

byMuhammad Mohsan Khan •Wednesday, November 20, 2024
0

 




  const [options, setOptions] = useState([
    { id: 1, name: 'Mohsan', time: '11:00 AM' },
    { id: 2, name: 'John', time: '12:00 PM' },
    { id: 3, name: 'Alex', time: '1:00 PM' },
    { id: 4, name: 'Sarah', time: '2:00 PM' },
    { id: 5, name: 'David', time: '3:00 PM' },
    { id: 6, name: 'Emma', time: '4:00 PM' },
    { id: 7, name: 'Michael', time: '5:00 PM' },
    { id: 8, name: 'James', time: '6:00 PM' },
    { id: 9, name: 'Liam', time: '7:00 PM' },
    { id: 10, name: 'Noah', time: '8:00 PM' },
    { id: 11, name: 'Olivia', time: '9:00 PM' },
    { id: 12, name: 'Sophia', time: '10:00 PM' },
    { id: 13, name: 'Ava', time: '11:00 PM' },
  ]);

  const [selectedOption, setSelectedOption] = useState(() => {
    const savedOption = localStorage.getItem('selectedOption');
    if (savedOption) {
      try {
        return JSON.parse(savedOption);
      } catch (e) {
        console.error('Error parsing savedOption:', e);
        return options[0]; // Fallback to the first option
      }
    }
    return options[0]; // Default to the first option
  });

  const [remainingTime, setRemainingTime] = useState('');
  const [closingTime, setClosingTime] = useState('');

  // Function to calculate the remaining time or "Closed" for today
  const calculateTimeRemaining = (selectedTime) => {
    const currentTime = new Date();
    const currentDate = currentTime.toLocaleDateString();
    const currentTimeInSeconds = currentTime.getTime() / 1000;

    const selectedOptionTime = new Date(`${currentDate} ${selectedTime}`);
    const selectedTimeInSeconds = selectedOptionTime.getTime() / 1000;

    if (selectedTimeInSeconds <= currentTimeInSeconds) {
      return 'Closed';
    }

    const timeRemaining = selectedTimeInSeconds - currentTimeInSeconds;
    const hours = Math.floor(timeRemaining / 3600);
    const minutes = Math.floor((timeRemaining % 3600) / 60);
    const seconds = Math.floor(timeRemaining % 60);

    return `${hours}hrs ${minutes}mins ${seconds}sec left`;
  };

  // Function to calculate closing time (9 minutes before selected time)
  const calculateClosingTime = (selectedTime) => {
    const currentDate = new Date().toLocaleDateString();
    const selectedOptionTime = new Date(`${currentDate} ${selectedTime}`);
    selectedOptionTime.setMinutes(selectedOptionTime.getMinutes() - 9);
    return selectedOptionTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  };

  // Function to handle option selection
  const handleOptionSelect = (option) => {
    setSelectedOption(option);
    localStorage.setItem('selectedOption', JSON.stringify(option)); // Save selected option in localStorage
    const time = calculateTimeRemaining(option.time);
    setRemainingTime(time);
    const closing = calculateClosingTime(option.time);
    setClosingTime(closing);
  };

  // Handle automatic switching when the option has passed
  const autoSwitchOption = () => {
    const currentTime = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
    const nextOption = options.find((option) => {
      const optionTime = new Date(`${new Date().toLocaleDateString()} ${option.time}`).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
      return currentTime < optionTime;
    });

    if (nextOption && selectedOption && nextOption.id !== selectedOption.id) {
      handleOptionSelect(nextOption);
    }
  };

  // Update remaining time and handle automatic switching
  useEffect(() => {
    const interval = setInterval(() => {
      if (selectedOption) {
        const time = calculateTimeRemaining(selectedOption.time);
        setRemainingTime(time);

        if (time === 'Closed') {
          autoSwitchOption();
        }
      }
    }, 1000);

    return () => clearInterval(interval);
  }, [selectedOption, options]);

  // Reset the timer at midnight
  useEffect(() => {
    const now = new Date();
    const nextDay = new Date(now);
    nextDay.setDate(now.getDate() + 1);
    nextDay.setHours(0, 0, 0, 0);

    const timeUntilMidnight = nextDay.getTime() - now.getTime();

    const resetTimer = setTimeout(() => {
      setRemainingTime('');
    }, timeUntilMidnight);

    return () => clearTimeout(resetTimer);
  }, []);
  • Facebook
  • Twitter
You may like these posts
Post a Comment (0)
Previous Post Next Post

Popular Posts

Unlock Your Coding Potential: A Comprehensive Guide to Learning JavaScript with Codecademy

From Beginner to Pro: Mastering JavaScript with Codecademy's Interactive Course

I want to start trading, but how do I choose a good broker?

Main Tags

  • ai
  • airdrop
  • android
  • androidproblem
  • androids
  • androidsolution
  • chatgpt
  • company
  • crypto
  • cryptoairdrop
  • cryptotrading
  • css
  • day
  • dinner
  • earnmoney
  • envirement
  • Finance
  • food
  • forextrading
  • free crypto
  • gardening
  • happy
  • health
  • healtylife
  • home
  • html
  • javascript
  • life
  • LLC
  • love
  • makemoney
  • makemoneyonline
  • makemoneywithinternet
  • mentalhealth
  • mobile
  • onlineearnmoney
  • power
  • recipes
  • remotework
  • self-discovery
  • slefcare
  • smile
  • stock market
  • technology
  • trading
Powered by Blogger

Поиск по этому блогу

Categories

  • ai(1)
  • airdrop(2)
  • android(2)
  • androidproblem(1)
  • androids(1)
  • androidsolution(1)
  • chatgpt(1)
  • company(1)
  • crypto(2)
  • cryptoairdrop(1)
  • cryptotrading(2)
  • css(3)
  • day(1)
  • dinner(1)
  • earnmoney(4)
  • envirement(1)
  • Finance(2)
  • food(6)
  • forextrading(10)
  • free crypto(2)
  • gardening(1)
  • happy(1)
  • health(13)
  • healtylife(5)
  • home(1)
  • html(6)
  • javascript(40)
  • life(6)
  • LLC(1)
  • love(1)
  • makemoney(8)
  • makemoneyonline(8)
  • makemoneywithinternet(7)
  • mentalhealth(5)
  • mobile(1)
  • onlineearnmoney(9)
  • power(1)
  • recipes(1)
  • remotework(1)
  • self-discovery(1)
  • slefcare(1)
  • smile(1)
  • stock market(12)
  • technology(2)
  • trading(12)

Popular Posts

  • (no title)

Popular Posts

Wednesday, January 08, 2025
Design by - Blogger Templates
  • Home
  • About
  • Privacy
  • Contact Us
  • Disclaimer
  • Term And Condition

Contact Form