|
Как подключить или просто кодам написать следующий таймер?
Есть таймер
function timer(elem, min, sec) {
(--sec < 0) && (sec = min-- ? 59 : 0);
min = Math.max(min, 0);
elem.innerHTML = min + " : " + sec;
if (sec || min)
setTimeout(timer.bind(0, elem, min, sec), 1000);
else {
alert('Время вышло!');
location.href = "/rekord.html";
}
}
timer(document.getElementById('play_timer'), 3, 0);
//Время в игре
function tick() {
var nowTime = new Date();
console.log('прошло секунд: ' + (nowTime + timer) / 1000);
А вот нашел таймер который записывает данный в локал стореч(на основе стандартных JQuery )
var interval;
let minutes = 1;
let currentTime = localStorage.getItem('currentTime');
let targetTime = localStorage.getItem('targetTime');
if (targetTime == null && currentTime == null) {
currentTime = new Date();
targetTime = new Date(currentTime.getTime() + (minutes * 60000));
localStorage.setItem('currentTime', currentTime);
localStorage.setItem('targetTime', targetTime);
}
else{
currentTime = new Date(currentTime);
targetTime = new Date(targetTime);
}
if(!checkComplete()){
interval = setInterval(checkComplete, 1000);
}
function checkComplete() {
if (currentTime > targetTime) {
clearInterval(interval);
alert("Time is up");
} else {
currentTime = new Date();
document.write(
"\n <font color=\"white\"> Seconds Remaining:" + ((targetTime - currentTime) / 1000) + "</font>");
}
}
document.onbeforeunload = function(){
localStorage.setItem('currentTime', currentTime);
}
Подскажите, как переделать, так что бы он работал без подключения просто на JS. |
Pavel_16,
:-? |
Цитата:
все продолжаю бороться с тем же ,но осталось только захватить киким-то образом время от начала загрузки страницы и поместить его в стореч. Но таймер должен отображаться для пользователя.. |
Pavel_16,
я не понимаю, что вы пишите, в чём проблема? |
Pavel_16,
if ([...elements].every(ep => ep.getAttribute('fill') != '#ffffff')) {
tick();
let time = document.getElementById('play_timer').innerHTML;
// здесь сохраняйте куда вам нужно
setTimeout(() => alert('Вы всё закрасили!'));
/*location.href = "/rekord.html";*/
}
|
Цитата:
Цитата:
"use strict";
$(document).ready(function(){
if (localStorage.getItem("users") === null) {
$.getJSON('Storage/users.json', function(users) {
localStorage.setItem("users", JSON.stringify(users));
});
}
var users = localStorage.getItem("users");
var $select = $('#name');
$select.ready(function(event){
$.each(JSON.parse(users), function (key, value) {
$('<option>').val(value.name).text(value.name).appendTo($select);
});
});
$('#play_game').click(function() {
var userName = $('#new_name').val();
var newUser = {name:userName};
users = JSON.parse(users);
users.push(newUser);
console.log(users);
localStorage.setItem("users", JSON.stringify(users));
});
});
|
Pavel_16,
надо обьединять скрипты, чтоб имя было доступно в игре ... как то так ...
if ([...elements].every(ep => ep.getAttribute('fill') != '#ffffff')) {
tick();
let time = document.getElementById('play_timer').innerHTML;
newUser.time = time;
localStorage.setItem("users", JSON.stringify(users));
// здесь вывод таблицы на основе обьекта users
setTimeout(() => alert('Вы всё закрасили!'));
/*location.href = "/rekord.html";*/
}
|
Цитата:
svg.js:54 Uncaught ReferenceError: newUser is not defined at SVGPathElement.handleClick (svg.js:54) handleClick @ svg.js:54 + до это-го прошло секунд: NaN |
Цитата:
Цитата:
|
Pavel_16,
var userName и var newUser нужно сделать глобальными |
| Часовой пояс GMT +3, время: 06:58. |
|