сохранение cookie javascript

207
03 июня 2018, 16:50

привет я хочу чтобы ответ result сохранился в cookie но чтобы он показался не с помочью alert а просто вывести как php <?php echo $_COOKIE["questi"]; ?>

var h1 = document.getElementsByTagName('h1')[0], 
  start = document.getElementById('start'), 
  stop = document.getElementById('stop'), 
  clear = document.getElementById('clear'), 
  seconds = 0, 
  minutes = 0, 
  hours = 0, 
  result = document.getElementById('result'), 
 // x = document.cookie = result, // Commented 
  t; 
 
 
function add() { 
  seconds++; 
  if (seconds >= 60) { 
    seconds = 0; 
    minutes++; 
    if (minutes >= 60) { 
      minutes = 0; 
      hours++; 
    } 
  } 
 
  h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : 
    "00") + ":" + (seconds > 9 ? seconds : "0" + seconds); 
 
  timer(); 
} 
 
function timer() { 
  t = setTimeout(add, 1000); 
} 
timer(); 
 
 
/* Start button */ 
start.onclick = timer; 
 
/* Stop button */ 
stop.onclick = function() { 
  clearTimeout(t) 
  time(); 
 
} 
 
/* Clear button */ 
clear.onclick = function() { 
  h1.textContent = "00:00:00"; 
  seconds = 0; 
  minutes = 0; 
  hours = 0; 
} 
 
function time() { 
 
  if (seconds >= 1 && seconds <= 30) { 
    result.textContent = 1; 
    result(); 
  } 
  if (seconds >= 35 && seconds <= 40) { 
    result.textContent = 9; 
  } 
 
  result(); 
 // document.cookie = result; // commented 
}
<h1><time>00:00:00</time></h1> 
<button id="start">start</button> 
<button id="stop">stop</button> 
<button id="clear">clear</button> 
<div id="result"></div>

Answer 1

Вот так:

var h1 = document.getElementsByTagName('h1')[0], 
  start = document.getElementById('start'), 
  stop = document.getElementById('stop'), 
  clear = document.getElementById('clear'), 
  seconds = 0, 
  minutes = 0, 
  hours = 0, 
  result = document.getElementById('result'), 
 // x = document.cookie = result, // Commented 
  t; 
 
 
function add() { 
  seconds++; 
  if (seconds >= 60) { 
    seconds = 0; 
    minutes++; 
    if (minutes >= 60) { 
      minutes = 0; 
      hours++; 
    } 
  } 
 
  h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : 
    "00") + ":" + (seconds > 9 ? seconds : "0" + seconds); 
 
  timer(); 
} 
 
function timer() { 
  t = setTimeout(add, 1000); 
} 
timer(); 
 
 
/* Start button */ 
start.onclick = timer; 
 
/* Stop button */ 
stop.onclick = function() { 
  clearTimeout(t) 
  time(); 
 
} 
 
/* Clear button */ 
clear.onclick = function() { 
  h1.textContent = "00:00:00"; 
  seconds = 0; 
  minutes = 0; 
  hours = 0; 
  // Если нужно почистить и значение в поле result то: 
  result.innerHTML = ''; 
} 
 
function time() { 
 
  if (seconds >= 1 && seconds <= 30) { 
    result.innerHTML = '1'; 
    //result(); 
  } 
  if (seconds >= 35 && seconds <= 40) { 
    result.innerHTML = '9'; 
  } 
 
  //result(); - это не функция, будет ошибка. 
 // document.cookie = result; // commented 
}
<h1><time>00:00:00</time></h1> 
<button id="start">start</button> 
<button id="stop">stop</button> 
<button id="clear">clear</button> 
<div id="result"></div>

READ ALSO
Один результат на букву DLE?

Один результат на букву DLE?

Скажите пожалуйста как сделать один результат на букву в /catalog/L/Сейчас выводятся все новости на букву и идет дубликат Я допустим добавил 4 трека

195
Программа не заполняет массив как нужно

Программа не заполняет массив как нужно

Ввести с клавиатуры целую матрицу размерностью 5x5Определить: 1) сумму положительных элементов в четных строках; 2) номера столбцов, не содержащих...

247
Библиотека для записи файла С++

Библиотека для записи файла С++

Усовершенствовать код с++Подскажите, пожалуйста, как будет правильнее записывать файл такой структуры

208