مشروع مؤقت التركيز بالجافاسكريبت: الكود كامل + شرح

مشروع مؤقت التركيز بالجافاسكريبت: الكود كامل + شرح

غلاف مشروع مؤقت التركيز بالجافاسكريبت

مشروع 2: مؤقت التركيز بجافاسكريبت من الصفر

نبني الواجهة، نحدد موعد النهاية، ونضيف بداية وإيقاف مؤقت واستكمال وإعادة ضبط، مع التحقق من المدة.

جرّب دقيقة، وقفها وكملها، وشوف الفرق بين إعادة الضبط والاستكمال.

تنبيه: التنبيه مرئي داخل الصفحة فقط، والمشروع لا يعمل بعد إغلاقها ولا يحتفظ بالجلسة بعد الريلود.

إيه اللي هتتعلمه في المشروع ده

هتتعلم تعمل مؤقت شغال بجد بـ setInterval والوقت الحقيقي، وتتحكم في حالة التشغيل والإيقاف والاستكمال، وتتحقق من المدة اللي المستخدم بيكتبها. الفكرة دي بتتكرر في أي تطبيق فيه عدّاد: ألعاب، اختبارات بوقت، أو تمارين رياضة.

إزاي الكود شغال

مفتاح الحكاية هو deadline: عند الضغط على ابدأ بنحسب Date.now() + remaining، وكل 200 ملي ثانية دالة tick بتحسب الوقت المتبقي من الفرق بين الموعد النهائي والوقت الحالي. الإيقاف المؤقت بيحفظ المتبقي، والاستكمال بيحسب موعد جديد منه. ومدة الجلسة لازم تكون رقم صحيح من 1 لـ 120 وإلا تظهر رسالة. وكمان visibilitychange بيحدّث العداد لما ترجع للتبويب.

غلطة شائعة

إنك تنقّص ثانية من عدّاد كل مرة setInterval تشتغل، على أساس إنها دقيقة. المتصفح ممكن يبطّئ التبويب في الخلفية فالمؤقت يتأخر. الحساب من الوقت الحقيقي بيحل المشكلة دي.

تمرين تكمل بيه

ضيف زرار استراحة قصيرة 5 دقايق بيشغّل المؤقت على مدة مختلفة. وبعدها خلّي آخر مدة اخترتها تتحفظ وتترجع لما تفتح الصفحة.

طريقة التشغيل

  1. اعمل مجلد جديد وسمّه project2.
  2. انسخ كل ملف من الأسفل واحفظه بنفس اسمه داخل المجلد.
  3. افتح index.html في متصفح حديث. أو شغّل خادم محلي من المجلد الذي يحتوي project2:
    python3 -m http.server 8000
    على ويندوز: py -m http.server 8000
    وافتح: http://localhost:8000/project2/

index.html

<!doctype html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>مؤقت التركيز | Ayoub EduTech</title><link rel="stylesheet" href="style.css">
<script src="app.js" defer></script></head><body><main>
<small>Ayoub EduTech • مشروع 02</small><h1>مؤقت التركيز</h1>
<section><label>مدة الجلسة بالدقائق، من 1 إلى 120
<input id="minutes" type="number" min="1" max="120" value="25"></label>
<button id="apply">تطبيق المدة</button><div id="clock" role="timer" aria-live="off">25:00</div>
<div class="row"><button id="start">ابدأ / استكمل</button><button id="pause">إيقاف مؤقت</button><button id="reset">إعادة ضبط</button></div>
<small>التنبيه مرئي داخل الصفحة. لا صوت ولا إشعارات خارجية.</small></section>
<p id="status" role="status" aria-live="polite"></p>
<small>مشروع تعليمي محلي • JavaScript</small></main></body></html>

style.css

* {
  box-sizing:border-box
}
body {
  margin:0;
  background:#140b26;
  color:#f7f2ff;
  font:18px "Noto Sans Arabic",Arial;
  direction:rtl
}
main {
  max-width:740px;
  margin:32px auto;
  padding:24px
}
h1 {
  font-size:30px;
  margin:8px 0
}
small {
  color:#c5b2de
}
section,article {
  background:#24133d;
  border:1px solid #614480;
  border-radius:18px;
  padding:20px;
  margin:18px 0
}
label {
  display:block;
  margin:12px 0
}
input,select,button {
  font:inherit;
  border-radius:10px;
  padding:10px;
  border:1px solid #8867a7
}
input,select {
  background:#10091b;
  color:white;
  width:100%
}
button {
  background:#f5ca52;
  color:#211234;
  cursor:pointer;
  margin:5px
}
button:disabled {
  opacity:.45;
  cursor:not-allowed
}
button:focus-visible,input:focus-visible,select:focus-visible {
  outline:3px solid #68e1d2
}
output,strong {
  color:#7ce6d5
}
.row {
  display:flex;
  gap:10px;
  align-items:center;
  justify-content:space-between
}
.row>* {
  min-width:0
}
ul {
  padding:0;
  list-style:none
}
li {
  padding:12px 0;
  border-bottom:1px solid #59416c
}
.error {
  color:#ffb6bb
}
#status {
  min-height:32px
}
#clock {
  font:64px monospace;
  text-align:center;
  direction:ltr
}
#choices button {
  display:block;
  width:100%;
  text-align:right
}
#choices button.correct {
  background:#68d7b8
}
#choices button.wrong {
  background:#ee9ca9
}
#products article {
  margin:10px 0
}
a {
  color:#7ce6d5
}
@media(max-width:500px) {
  main {
    padding:16px;
    margin:10px auto
  }
  .row {
    flex-wrap:wrap
  }
  h1 {
    font-size:26px
  }
}

app.js

const $ = id => document.getElementById(id);
let duration = 25 * 60 * 1000;
let remaining = duration;
let deadline = 0;
let running = false;
let interval = null;
function render() {
  const seconds = Math.ceil(remaining / 1000);
  const minutes = String(Math.floor(seconds / 60)).padStart(2, '0');
  $('clock').textContent = `${minutes}:${String(seconds % 60).padStart(2, '0')}`;
  $('start').disabled = running || remaining === 0;
  $('pause').disabled = !running;
}
function stop() {
  clearInterval(interval); interval = null; running = false;
}
function tick() {
  if (!running) return;
  remaining = Math.max(0, deadline - Date.now());
  if (remaining === 0) {
    stop(); $('status').textContent = 'انتهت الجلسة. خذ استراحة!';
  }
  render();
}
$('start').addEventListener('click', () => {
  if (running || remaining === 0) return;
  deadline = Date.now() + remaining;
  running = true; interval = setInterval(tick, 200);
  $('status').textContent = 'جلسة تركيز جارية'; render();
});
$('pause').addEventListener('click', () => {
  tick(); if (!running) return;
  stop(); $('status').textContent = 'الجلسة متوقفة مؤقتا'; render();
});
$('reset').addEventListener('click', () => {
  stop(); remaining = duration;
  $('status').textContent = 'جاهز لجلسة جديدة'; render();
});
$('apply').addEventListener('click', () => {
  const raw = $('minutes').value.trim();
  const minutes = Number(raw);
  if (!raw || !Number.isInteger(minutes) || minutes < 1 || minutes > 120) {
    $('status').textContent = 'اكتب عددا صحيحا من 1 إلى 120.'; return;
  }
  stop(); duration = minutes * 60000; remaining = duration;
  $('status').textContent = 'تم تطبيق المدة. اضغط ابدأ.'; render();
});
document.addEventListener('visibilitychange', tick);
render();

تحميل المشاريع

تحميل ملف ZIP للمشاريع الخمسة (Google Drive)

تابع Ayoub EduTech وطبّق بإيدك.