Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   Помогите изменить живой поиск (https://javascript.ru/forum/events/76107-pomogite-izmenit-zhivojj-poisk.html)

Никита Белан 05.12.2018 21:35

Помогите изменить живой поиск
 
Вложений: 1
Такая проблема - при вводе поисковой фразы, с третьей буквы, поиск выдает результат, и если продолжить вводить символы, результат пропадает, а потом опять выскакивает, жутко раздражает. Как сделать что-бы результат поиска не пропадал, а либо всегда был виден, либо поставить задержку (хотя бы секунду)? Сайт https://vest.in.ua/. Я js только начал изучать, буду благодарен за помощь. Как я понял за поиск отвечает этот код, приложу файл с кодом в txt формате:

function doLiveSearch(a, b) {
	return 38 != a.keyCode && 40 != a.keyCode && ($("#livesearch_search_results").remove(), updown = -1, !("" == b || b.length < 3 || (b = encodeURI(b), $.ajax({
		url: $("base").attr("href") + "index.php?route=product/search/ajax&keyword=" + b + "&filter_category_id=" + $('#search input[name=category_id]').val(),
		dataType: "json",
		success: function (a) {
			if (a.length > 0) {
				var c = document.createElement("ul");
				c.id = "livesearch_search_results";
				var d, e;
				for (var f in a) {
					if (d = document.createElement("li"), eListHr = document.createElement("hr"), eListDiv = document.createElement("div"), eListDiv.setAttribute("style", "height: 10px; clear: both;"), eListDivpr = document.createElement("span"), eListDivpr.innerHTML = (a[f].price) ? a[f].price : '', eListDivpr.setAttribute("style", "height: 14px; color: #147927;"), eListDivprspec = document.createElement("span"), eListDivprspec.innerHTML = a[f].special, eListDivprspec.setAttribute("style", "font-weight: bold; margin-left: 8px; color: #a70d0d; font-size: 16px;"), eListImg = document.createElement("img"), eListImg.src = a[f].image, eListImg.setAttribute("style", "margin-right: 10px;"), eListImg.align = "left", eListDivstatus = document.createElement("span"), eListDivstatus.innerHTML = a[f].stock, eListDivstatus.setAttribute("style", "height: 14px; color: #337ab7; margin-left: 15px; font-weight: bold;"), e = document.createElement("a"), e.setAttribute("style", "display: block;"), e.appendChild(document.createTextNode(a[f].name)), "undefined" != typeof a[f].href) {
						"" != a[f].special && eListDivpr.setAttribute("style", "text-decoration: line-through;");
						var g = decodeURIComponent(a[f].href);
						
						e.href = g.replace('&amp;', "&");
					} else e.href = $("base").attr("href") + "index.php?route=product/product&product_id=" + a[f].product_id + "&keyword=" + b;
					d.appendChild(e), c.appendChild(eListImg), c.appendChild(d), c.appendChild(eListDivpr), "" != a[f].special && c.appendChild(eListDivprspec), c.appendChild(eListDivstatus), c.appendChild(eListHr), c.appendChild(eListDiv)
				}
				$("#livesearch_search_results").length > 0 && $("#livesearch_search_results").remove(), $("#search").append(c), $("#livesearch_search_results").css("display", "none"), $("#livesearch_search_results").slideDown("slow"), $("#livesearch_search_results").animate({
					backgroundColor: "rgba(255, 255, 255, 0.98)"
				}, 2e3)
			}
		}
	}), 0)))
}


function upDownEvent(a) {
	var b = document.getElementById("livesearch_search_results");
	if ($("#search").find("[name=search]").first(), b) {
		var c = b.childNodes.length - 1;
		if (updown != -1 && "undefined" != typeof b.childNodes[updown] && $(b.childNodes[updown]).removeClass("highlighted"), 38 == a.keyCode ? updown = updown > 0 ? --updown : updown : 40 == a.keyCode && (updown = updown < c ? ++updown : updown), updown >= 0 && updown <= c) {
			$(b.childNodes[updown]).addClass("highlighted");
			var d = b.childNodes[updown].childNodes[0].text;
			"undefined" == typeof d && (d = b.childNodes[updown].childNodes[0].innerText), $("#search").find("[name=search]").first().val(new String(d).replace(/(\s\(.*?\))$/, ""))
		}
	}
	return !1
}


$("#search").find("[name=search]").first().keyup(function (a) {
		doLiveSearch(a, this.value)
	}).focus(function (a) {
		doLiveSearch(a, this.value)
	}).keydown(function (a) {
		upDownEvent(a)
	}).blur(function () {
		window.setTimeout("$('#livesearch_search_results').remove();updown=0;", 1500)
	}), $(document).bind("keydown", function (a) {
		try {
			13 == a.keyCode && $(".highlighted").length > 0 && (document.location.href = $(".highlighted").find("a").first().attr("href"))
		} catch (a) {}
	});


Часовой пояс GMT +3, время: 16:33.