Skip to content

Instantly share code, notes, and snippets.

@raveman
Last active October 7, 2022 08:28
Show Gist options
  • Save raveman/a5548be75ef3e98242c74ff0e720f7f5 to your computer and use it in GitHub Desktop.
Save raveman/a5548be75ef3e98242c74ff0e720f7f5 to your computer and use it in GitHub Desktop.
<div class="panel" id="live_check">
<div class="panel-heading text-center">
<h2>Контроль участия</h2>
</div>
<div class="panel-body">
<p class="question">Вы с нами в прямом эфире ?</p>
<%= link_to 'Да', '#', onclick: 'javascript:close_live_check(); return;', class: 'btn btn-info btn-block' %>
</div>
</div>
<script type="text/javascript">
function get_checked_times() {
var checkedTimes = parseInt(getCookie('live_checked_times'));
if (isNaN(checkedTimes)) {
checkedTimes = 0;
setCookie('live_checked_times', checkedTimes);
}
return checkedTimes;
}
function display_live_check(popup, dateTime) {
$("#broadcast").append(popup);
}
(function live_check_popup_worker() {
var popup = "<%= j render partial: 'stats/check_popup' %>";
display_live_check();
var checker = document.getElementById("live_check");
var today = new Date();
var todayDate = new Date(today.getUTCFullYear() + "/" + (today.getUTCMonth() + 1) + "/" + today.getUTCDate());
var timeStart = new Date(today.getUTCFullYear() + "/" + (today.getUTCMonth() + 1) + "/" + today.getUTCDate() + " " + '06:50 UTC');
var timeEnd = new Date(today.getUTCFullYear() + "/" + (today.getUTCMonth() + 1) + "/" + today.getUTCDate() + " " + '15:00 UTC');
if (checker === null) {
if (today > timeStart && today < timeEnd) {
var now = new Date();
var doCheck = false;
var lastCheckCookie = getCookie('live_checked_last');
if (lastCheckCookie === "") {
lastCheck = now;
} else {
var lastCheck = new Date(lastCheckCookie);
}
var delayInSeconds = 1000*60*40; // 40 minutes in miliseconds
var delta = now.getTime() - lastCheck.getTime();
if (delta > delayInSeconds || delta === 0) {
doCheck = true;
}
var lastCheckDate = new Date(lastCheck.getUTCFullYear() + "/" + (lastCheck.getUTCMonth() + 1) + "/" + lastCheck.getUTCDate());
// (lastCheckDate.getTime() === todayDate.getTime()) &&
if (doCheck) {
display_live_check(popup, now);
}
}
}
// setTimeout(live_check_popup_worker, 120000); // checking every 2 minutes our interval
setTimeout(live_check_popup_worker, 1000*60*40); // opening popup every 40 minutes in milisecods
})();
(function live_check_worker() {
$.ajax({
url: '<%= stat_live_check_path(user_id: current_user.id, seminar_id: @seminar.id, kind: 1) %>',
statusCode: {
401: function () {
},
403: function () {
}
},
success: function (data) {
}
});
setTimeout(live_check_worker, 600000); // checking every 10 minutes
})();
function close_live_check() {
$.ajax({
url: '<%= stat_live_check_path(user_id: current_user.id, seminar_id: @seminar.id, kind: 2) %>',
statusCode: {
401: function () {
},
403: function () {
}
},
success: function (data) {
var checkedTimes = get_checked_times();
if (checkedTimes >= 0) {
var now = new Date();
setCookie('live_checked_last', now);
setCookie('live_checked_times', checkedTimes + 1);
}
}
});
$("#live_check").fadeOut().remove();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment