Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript][PHP]Przerwanie wykonywania funkcji i ponowne uruchomienie.
ilidir
post 8.12.2021, 08:33:55
Post #1





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Witam mam poniższy kod
  1. $(function() {
  2. function timeChecker() {
  3. var timeStamp = new Date();
  4. sessionStorage.setItem("lastTimeStamp", timeStamp);
  5. setInterval(function() {
  6. var storedTimeStamp = sessionStorage.getItem("lastTimeStamp");
  7. timeCompare(storedTimeStamp);
  8. }, 3000);
  9. }
  10.  
  11. function timeCompare(timeString){
  12. var currenTime = new Date();
  13. var pastTime = new Date(timeString);
  14. var timeDiff = currenTime - pastTime;
  15. var minPast = Math.floor((timeDiff/60000));
  16. //console.log(pastTime +" "+ timeDiff +" "+ minPast);
  17.  
  18. if(minPast > 90) {
  19. var log_url = "<?php $logout = wp_logout_url($redirect); echo $logout; ?>"
  20. sessionStorage.removeItem("lastTimeStamp");
  21. window.location.replace(log_url);
  22. return false;
  23.  
  24.  
  25. }else {
  26. //console.log(currenTime +" - "+pastTime+" - "+minPast+ " min");
  27. }
  28.  
  29. }
  30.  
  31. $(document).mousemove(function(){
  32. var timeStamp = new Date();
  33. sessionStorage.setItem("lastTimeStamp", timeStamp);
  34.  
  35. });
  36. timeChecker();
  37.  
  38. });
  39.  



Funkcja startuje na końcu. Jednakże ja chciałbym ja zatrzymać w innej funkcji a następnie ja wywołać z tej funkcji dopiero.

Będę wdzięczny za pomoc. Pozdrawiam
Go to the top of the page
+Quote Post
trueblue
post 8.12.2021, 11:18:04
Post #2





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


To w jakim celu ją startujesz na końcu? Nie możesz po prostu wywołać w innej funkcji?


--------------------
Go to the top of the page
+Quote Post
ilidir
post 8.12.2021, 11:37:52
Post #3





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Jeżeli normalny użytkownik wchodzi na stronę i ją przegląda to chciałbym aby dla bezpieczeństwa po upływanie 91 minut bez ruchu nastąpiło auto wylogowanie. Jednakże jeżeli odpali podstronę (na przykład stronę z Video) w ramach przeglądanej strony to chciałbym żeby funkcja się przerwała bo na przykład film może trwać 100 minut a po 91 go wyloguje jeżeli nie ruszy myszką.

Ten post edytował ilidir 8.12.2021, 11:38:46
Go to the top of the page
+Quote Post
trueblue
post 8.12.2021, 12:22:30
Post #4





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Tu jest podobny problem: Temat: Zatrzymanie setinterval w przykladzie


--------------------
Go to the top of the page
+Quote Post
ilidir
post 8.12.2021, 12:39:12
Post #5





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Cytat(trueblue @ 8.12.2021, 12:22:30 ) *


Dzięki,

czyli generalnie powinienem wywołać

clearInterval(timeChecker);

i to powinno zatrzymać wykonywanie funkcji.

pozdrawiam
Go to the top of the page
+Quote Post
trueblue
post 8.12.2021, 13:05:34
Post #6





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Generalnie, nie.
Przeczytaj jeszcze raz tamten temat.


--------------------
Go to the top of the page
+Quote Post
ilidir
post 9.12.2021, 10:25:26
Post #7





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


no tak nie funkcja a licznik interwalu

clearInterval(storedTimeStamp);

Generalnie mam problem bo nie wiem gdzie wywołać to czyszczenie żeby działało poprawnie.
Go to the top of the page
+Quote Post
trueblue
post 9.12.2021, 10:40:40
Post #8





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Generalnie tam gdzie opisywałeś, czyli kiedy odpalasz podstronę, np. z video, w ramach przeglądanej strony.


--------------------
Go to the top of the page
+Quote Post
ilidir
post 9.12.2021, 11:14:03
Post #9





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


ale to mam żywcem wywołać clearInterval(storedTimeStamp); questionmark.gif
Go to the top of the page
+Quote Post
trueblue
post 9.12.2021, 11:34:42
Post #10





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Nie wiem co masz na myśli pisząc "żywcem". Jeśli licznik ma się zatrzymać, to trzeba wywołać clearInterval, ale na pewno nie z parametrem storedTimeStamp.


--------------------
Go to the top of the page
+Quote Post
ilidir
post 9.12.2021, 12:25:13
Post #11





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Mhm, dzięki

czyli może powinienem zrobić tak:
Kod
var interval = setInterval(function() {
            var storedTimeStamp = sessionStorage.getItem("lastTimeStamp");
            timeCompare(storedTimeStamp);
        }, 3000);


a później wywołać:
clearInterval(interval);

questionmark.gif?
Go to the top of the page
+Quote Post
com
post 9.12.2021, 12:31:35
Post #12





Grupa: Zarejestrowani
Postów: 3 032
Pomógł: 366
Dołączył: 24.05.2012

Ostrzeżenie: (0%)
-----


no brawo
https://www.medianauka.pl/clearInterval-javascript smile.gif
Go to the top of the page
+Quote Post
ilidir
post 9.12.2021, 13:57:13
Post #13





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Jak wywołuje to czyszczenie z poziomu innej funkcji to mam komunikat:

Uncaught ReferenceError: interval is not defined
Go to the top of the page
+Quote Post
trueblue
post 9.12.2021, 14:01:01
Post #14





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


https://developer.mozilla.org/pl/docs/Web/J...C4%99g_zmiennej


--------------------
Go to the top of the page
+Quote Post
ilidir
post 9.12.2021, 14:41:43
Post #15





Grupa: Zarejestrowani
Postów: 183
Pomógł: 14
Dołączył: 27.09.2014

Ostrzeżenie: (0%)
-----


Poszło dziękuje smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 14:53