Last active
March 26, 2018 14:24
-
-
Save MrShennawy/80753798019a9b11109279a1128deee5 to your computer and use it in GitHub Desktop.
Run a specific event at a certain time just Js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>تجربة تشغيل حدث معين فى وقت معين</title> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<span class="timePrayer" style="color:#004dff;" data-time="05:20 AM"> الوقت الأول </span><br> | |
<span class="timePrayer" style="color:#004dff;" data-time="11:55 AM"> الوقت الثانى </span><br> | |
<span class="timePrayer" style="color:#004dff;" data-time="02:55 PM"> الوقت الثالث </span> | |
<span class="timePrayer" style="color:#004dff;" data-time="05:15 PM"> الوقت الرابع </span> | |
<span class="timePrayer" style="color:#004dff;" data-time="6:25 PM"> الوقت الخامس </span> | |
</body> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> | |
<script src="script.js"></script> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( document ).ready(function() { | |
setInterval(function(){ | |
$('.timePrayer').each(function(){ | |
var startTime = $(this).data('time'); | |
prayer(startTime); | |
}); | |
}, 60000); | |
function prayer(startTime){ | |
var curr_time = getval(); | |
if (get24Hr(curr_time) == get24Hr(startTime)) { | |
// الحدث المراد تنفيذه | |
alert("حان الوقت") | |
}else{ | |
// فى حالة إختلاف الوقت | |
alert("No") | |
} | |
function get24Hr(time){ | |
var hours = Number(time.match(/^(\d+)/)[1]); | |
var AMPM = time.match(/\s(.*)$/)[1]; | |
if(AMPM == "PM" && hours<12) hours = hours+12; | |
if(AMPM == "AM" && hours==12) hours = hours-12; | |
var minutes = Number(time.match(/:(\d+)/)[1]); | |
hours = hours*100+minutes; | |
return hours; | |
} | |
function getval() { | |
var currentTime = new Date() | |
var hours = currentTime.getHours() | |
var minutes = currentTime.getMinutes() | |
if (minutes < 10) minutes = "0" + minutes; | |
var suffix = "AM"; | |
if (hours >= 12) { | |
suffix = "PM"; | |
hours = hours - 12; | |
} | |
if (hours == 0) { | |
hours = 12; | |
} | |
var current_time = hours + ":" + minutes + " " + suffix; | |
return current_time; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment