Co robię nie tak? |
Co robię nie tak? |
19.06.2022, 14:10:11
Post
#1
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
Mam taki kod:
Mimo tego, w kontenerku <div id="odpowiedz"> widzę cyfrę 1 zamiast tekstu "Podany przez Ciebie kod jest OK" Co robię nie tak? |
|
|
19.06.2022, 20:41:11
Post
#2
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
"1" !== 1
-------------------- |
|
|
19.07.2022, 13:46:20
Post
#3
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
Dzięki, działa. Klikam pomógł.
Jeszcze jeden problem z użyciem tego skryptu. Zdarza się, że w nocy lub w dzień z zniewiadomych powodów (pewnie dostawca interentu) nie dostanę odpowiedzi od skryptu ?ajax.php. ZAuważyłem, że wtedy odświeżanie danych co 30 sekund przestaje działać. Zmieniałem kod
Dołożyłem
ale działa to tak, że tylko w czasie odświeżania ?ajax.php pojawia się na krótko "No connection with the Application" na żółoto a jeśli w ramach testu wyłącze apache2 na serwerze to nie mam informacji, że połączenie z aplikacją padło. Co robię nie tak? |
|
|
19.07.2022, 16:49:44
Post
#4
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
Sprawdź co otrzymujesz w xhr.status jeśli serwer nie działa.
Swoją drogą: https://www.w3schools.com/js/js_api_fetch.asp -------------------- |
|
|
19.07.2022, 19:07:19
Post
#5
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
|
|
|
19.07.2022, 20:17:54
Post
#6
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
Przecież zmienną xhr i jej własność status używasz w kodzie.
-------------------- |
|
|
19.07.2022, 22:17:21
Post
#7
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
Ok, dodalem coś takiego
Cytat ------>} else { <------> console.log(xhr.status); <------> devsDiv.innerHTML = '<div class="col-md-12 themed-grid-col text-white bg-warning">No connection with the Application</div>'; <------> setTimeout(updateLiveData, 30000); <------>} i obserwuję. Tak to teraz wygląda: W dodatku nie odczekuje tych 30 sekund i w kółko się odświeża. Ten post edytował DNMX 19.07.2022, 22:27:21 |
|
|
20.07.2022, 07:17:41
Post
#8
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
-------------------- |
|
|
20.07.2022, 09:21:16
Post
#9
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
|
|
|
20.07.2022, 09:25:18
Post
#10
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
Przecież z tego kodu dokładnie wynika gdzie dodać...
Pierwszą linię dodajesz tam gdzie jest błąd połączenia, bo przecież to ma wskazywać. Drugą ma cyklicznie wywoływać żądanie, czyli za każdym razem. -------------------- |
|
|
20.07.2022, 09:48:51
Post
#11
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
Nie dla każdego JS jest takie jasne jak dla Ciebie
Kod if(xhr.readyState == 4) if(xhr.status == 200) { console.log('ok'); setTimeout(updateLiveData, 30000); } else if(xhr.status == 0){ console.log('blad'); devsDiv.innerHTML = '<div class="col-md-12 themed-grid-col text-white bg-warning">No connection with the Application</div>'; setTimeout(updateLiveData, 30000); } } Czy w ósmej lini nie muszę już ustawiać timeoutu? |
|
|
20.07.2022, 09:58:02
Post
#12
|
|
Grupa: Zarejestrowani Postów: 6 783 Pomógł: 1826 Dołączył: 11.03.2014 Ostrzeżenie: (0%) |
Myślę, że nie ma to związku z JS tylko z zasadami programowania.
setTimeout ma być wykonany za każdym razem, więc trzeba go wyprowadzić poza warunki. -------------------- |
|
|
20.07.2022, 10:09:54
Post
#13
|
|
Grupa: Zarejestrowani Postów: 128 Pomógł: 0 Dołączył: 18.09.2021 Ostrzeżenie: (0%) |
Ogarnęłem, dzięki. Klikam pomógł!
Edit: Po czasie okazało się, że taki kod: Kod window.addEventListener('load', function() { var xhr = null; getXmlHttpRequestObject = function() { if(!xhr) { // Create a new XMLHttpRequest object. xhr = new XMLHttpRequest(); } return xhr; }; updateLiveData = function() { var now = new Date(); // Date string is appended as a query with live data. // for not to use the cached version. var url = '/?ajax'; xhr = getXmlHttpRequestObject(); xhr.onreadystatechange = evenHandler; // asynchronous requests xhr.open("GET", url, true); // Send the request over the network xhr.send(null); }; updateLiveData(); function evenHandler() { listsDiv = document.getElementById('lists'); devsDiv = document.getElementById('devices'); // Check response is ready or not if(xhr.readyState == 4) { if( xhr.status == 200) { const not = JSON.parse(xhr.responseText); // Set current data text listsDiv.innerHTML = not['lists']; devsDiv.innerHTML = not['devices']; var title = document.querySelector('meta[name="statictitle"]').content; //Notifications if(not['newlists'] > 0) { document.title = "(" + not['newlists'] + ") " + title; } else { document.title = title; } } else { devsDiv.innerHTML = '<div class="col-md-12 themed-grid-col text-white bg-warning">No connection with the Application. HTTP status: ' + xhr.status + '</div>'; } } else { devsDiv.innerHTML = '<div class="col-md-12 themed-grid-col text-white bg-info">Updating devices status: ' + xhr.readyState + '</div>'; } setTimeout(updateLiveData, 30000); } }); Po pewnym czasie zaczyna odświeżać sronę co sekundę mniej więcej. Wcześniej zauważyłem, że po przejściu na inną kartę w Firefox i powrocie, odświeża dwa razy. Czy ma to związek z przeglądarką? Ten post edytował DNMX 20.07.2022, 11:41:56 |
|
|
Wersja Lo-Fi | Aktualny czas: 27.09.2024 - 04:20 |