Спасибо, IllusionMH, спасибо SAD
Вот конечный вариант (мой, два типа сообщений, все работает)
1) сделал не через атрибут, а через свойство
2) Нужны оба кода, что писал SAD, так как сообщению назначается setInterval дважды: при вызове через промежуток времени (15 сек) и при "погасании" в течении 3 сек
3)при погасании убрал удаление timeoutId - оно все равно перезаписывается, а во втором блоке кода - не нужен.
Цитата:При передаче несуществующего или уже выполненного timeoutID - ничего не происходит, исключение не генерируется.
4) и самое главное: удаление timeoutId происходит при вызове hideElem (в моем случае "em") в начале
5) Повысил карму SAD до 46
Спойлер (Отобразить)CODE ( javascript):
скопировать код в буфер обмена
function em(d) { var tMax = 15000; var tMin = 30; var error = document.getElementById('error'); var message = document.getElementById('message'); var eTimer = error.timeoutId; if (typeof eTimer !== null) { clearTimeout(eTimer); } var mTimer = message.timeoutId; if (typeof mTimer !== null) { clearTimeout(mTimer); } error.innerHTML = ''; error.style.display = 'none'; message.innerHTML = ''; message.style.display = 'none'; if (typeof d !== 'object') { return false; } //Вывод ошибки: if (d.error !== undefined && d.error !== null) { error.style.display = 'block'; error.style.opacity = 1; for (i = 0; i < d.error.length; i++) { error.innerHTML += "<div class='error'>" + d.error[i] + "</div>"; } error.timeoutId = setTimeout(function() { fadeOut(error, tMin); }, tMax); return false; } else if (d.message !== undefined && d.message !== null) { message.style.display = 'block'; message.style.opacity = 1; for (i = 0; i < d.message.length; i++) { message.innerHTML += "<div class='message'>" + d.message[i] + "</div>"; } message.timeoutId = setTimeout(function() { fadeOut(message, tMin); }, tMax); } return true; } function fadeOut(elem, slow) { elem.style.opacity = elem.style.opacity - 0.01; if (elem.style.opacity > 0) { elem.timeoutId = setTimeout(function() { fadeOut(elem, slow); }, slow); } else { elem.style.display = 'none'; } }
|