Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][SQL][PHP]Pomoże mi ktoś zrobić krótki skrypt (timer)?
Forum PHP.pl > Forum > Przedszkole
MefjuU
Witam otóż chodzi mi o taki skrypt:
- że po 30 minutach pobytu na naszej stronie będzie nam dodawać do bazy danych punkty.
(Chodzi mi o sam TIMER bo resztę z bazą danych to se ogarnę).

Jedyny skrypt jaki znalazłem z TIMER'em to był taki co odlicza nam dni do końca roku, a mi chodzi o taki który po upływie 30 min. wykona zapis do bazy i ponownie zacznie odliczanie.
(najlepiej by było jakby miał jakieś zabezpieczenie że gdy na windowsie zmienimy godzine to nie wpłynie na ten skrypt). wink.gif
aras785
Cześć, w Twoim przypadku wystarczy zwykłe "odejmowanie" liczb, nie trzeba posługiwać się "czasem".

HTML:
  1. Odliczanie:
  2. <div id="test"></div>

JQUERY:

  1. /**
  2. * duration - podajemy w sekundach np. 30 * 60 = 30 minut :)
  3. * display - "element" w którym ma być wyświetlane odliczanie np. $('#test');
  4. */
  5. function startTimer(duration, display) {
  6. var timer = duration
  7. var minutes;
  8. var seconds;
  9. countDown();
  10. var interval = setInterval(countDown, 1000);
  11. function countDown() {
  12. minutes = parseInt(timer / 60, 10)
  13. seconds = parseInt(timer % 60, 10);
  14.  
  15. minutes = minutes < 10 ? "0" + minutes : minutes;
  16. seconds = seconds < 10 ? "0" + seconds : seconds;
  17.  
  18. display.text(minutes+":"+seconds);
  19.  
  20. if (--timer < 0) {
  21. display.text('Tutaj akcja!');
  22. clearInterval(interval);
  23. }
  24. }
  25. }
  26. $(function() {
  27. startTimer(30*60,$('#test'));
  28. })


https://jsfiddle.net/oo4ptd24/1/

Pozdro
Neutral
http://www.mysqltutorial.org/mysql-trigger...cheduled-event/

Pamiętaj, żeby to włączyć:

SET GLOBAL event_scheduler = ON;

https://developer.mozilla.org/en-US/docs/We...Getting_Started

http://phpmajster.blogspot.com/2015/06/aja...x-i-obiekt.html

https://dev.mysql.com/doc/refman/5.7/en/create-event.html

event_scheduler_ajax.php:
  1. <body style="background:grey">
  2.  
  3. <input type="text" id="time_to_starting"/>
  4. Format: xxxx-xx-xx xx:xx:xx
  5. <input type="text" id="time_to_stopped"/>
  6. <input type="number" id="numb"/>
  7. <button id="delete_sch">sch delete</button>
  8. <div id="timer"></div>
  9.  
  10. var sch = 'false';
  11. function delete_scheduler(){
  12. sch = 'true';
  13. }
  14.  
  15. document.getElementById('delete_sch').addEventListener('click',delete_scheduler,false);
  16.  
  17. var httpRequest;
  18. var timer = document.getElementById('timer');
  19.  
  20.  
  21.  
  22. function makeRequest() {
  23. httpRequest = new XMLHttpRequest();
  24.  
  25. if (!httpRequest) {
  26. alert('Giving up :( Cannot create an XMLHTTP instance');
  27. return false;
  28. }
  29. httpRequest.onreadystatechange = alertContents;
  30. httpRequest.open('POST', 'event_sch.php',true);
  31. httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  32. httpRequest.send('start='+document.getElementById('time_to_starting').value+'&stop='+document.getElementById('time_to_stopped').value+'&intervalx='+document.getElementById('numb').value+'&sch='+sch);
  33. }
  34.  
  35. function alertContents() {
  36. if (httpRequest.readyState === XMLHttpRequest.DONE) {
  37. if (httpRequest.status === 200) {
  38. var stopped = document.getElementById('timer').innerHTML = (httpRequest.responseText);
  39.  
  40.  
  41. if(timer.innerHTML.split(':')[0]==document.getElementById('time_to_stopped').value.split(':')[0]&&timer.innerHTML.split(':')[1]==document.getElementById('time_to_stopped').value.split(':')[1]&&timer.innerHTML.split(':')[2]==document.getElementById('time_to_stopped').value.split(':')[2]){
  42. clearInterval(interv);
  43. //console.log('true');
  44. }
  45. } else {
  46. alert('There was a problem with the request.');
  47. }
  48. }
  49. }
  50.  
  51. var interv = setInterval(makeRequest,1000);
  52.  

event_sch.php:
  1. <?php
  2.  
  3.  
  4. $conn = new mysqli('localhost','root','','turqus');
  5.  
  6. $query = 'select * from points;';
  7.  
  8. $query2= 'create event tictactoe on schedule
  9. every '."'{$_POST['intervalx']}'".' second
  10. starts '."'{$_POST['start']}'".'
  11. ends '."'{$_POST['stop']}'".'
  12. do
  13. update turqus.points set col = now();';
  14.  
  15. $resultx = $conn->query($query2);
  16.  
  17. $query3 = 'drop event tictactoe';
  18.  
  19. if($_POST['sch']=='true'){
  20.  
  21. $conn->query($query3);
  22.  
  23. }
  24.  
  25. // var_dump($resultx);
  26.  
  27. $result = $conn->query($query);
  28.  
  29. while($result2 = $result->fetch_assoc()){
  30.  
  31. echo $result2['col'];
  32.  
  33. }
  34.  
  35. ?>


  1. MariaDB [turqus]> SHOW CREATE TABLE points\G
  2. *************************** 1. row ***************************
  3. TABLE: points
  4. CREATE TABLE: CREATE TABLE `points` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `col` datetime DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1


Do tabeli wstaw jakieś dane, np. takie:

  1. INSERT INTO points VALUES(NULL,'2018-04-28 06:00:00');


Poprawienie tego kodu zgodnie z Twoimi oczekiwaniami zostawiam Tobie.
MefjuU
Cytat(aras785 @ 27.04.2018, 22:50:43 ) *
Cześć, w Twoim przypadku wystarczy zwykłe "odejmowanie" liczb, nie trzeba posługiwać się "czasem".

HTML:
  1. Odliczanie:
  2. <div id="test"></div>

JQUERY:

  1. /**
  2. * duration - podajemy w sekundach np. 30 * 60 = 30 minut :)
  3. * display - "element" w którym ma być wyświetlane odliczanie np. $('#test');
  4. */
  5. function startTimer(duration, display) {
  6. var timer = duration
  7. var minutes;
  8. var seconds;
  9. countDown();
  10. var interval = setInterval(countDown, 1000);
  11. function countDown() {
  12. minutes = parseInt(timer / 60, 10)
  13. seconds = parseInt(timer % 60, 10);
  14.  
  15. minutes = minutes < 10 ? "0" + minutes : minutes;
  16. seconds = seconds < 10 ? "0" + seconds : seconds;
  17.  
  18. display.text(minutes+":"+seconds);
  19.  
  20. if (--timer < 0) {
  21. display.text('Tutaj akcja!');
  22. clearInterval(interval);
  23. }
  24. }
  25. }
  26. $(function() {
  27. startTimer(30*60,$('#test'));
  28. })


https://jsfiddle.net/oo4ptd24/1/

Pozdro



Zrobiłem coś takiego do przetestowania:

  1. <html>
  2. <head>
  3. <script>
  4. /**
  5. * duration - podajemy w sekundach np. 30 * 60 = 30 minut :)
  6. * display - "element" w którym ma być wyświetlane odliczanie np. $('#test');
  7. */
  8. function startTimer(duration, display) {
  9. var timer = duration
  10. var minutes;
  11. var seconds;
  12. countDown();
  13. var interval = setInterval(countDown, 1000);
  14. function countDown() {
  15. minutes = parseInt(timer / 60, 10)
  16. seconds = parseInt(timer % 60, 10);
  17.  
  18. minutes = minutes < 10 ? "0" + minutes : minutes;
  19. seconds = seconds < 10 ? "0" + seconds : seconds;
  20.  
  21. display.text(minutes+":"+seconds);
  22.  
  23. if (--timer < 0) {
  24. display.text('Tutaj akcja!');
  25. clearInterval(interval);
  26. }
  27. }
  28. }
  29. $(function() {
  30. startTimer(30*60,$('#test'));
  31. })
  32. </script>
  33. </head>
  34. <body>
  35. Odliczanie: <div id="test"></div>
  36. </body>
  37. </html>


Ale nie chce mi wyświetlić tego czasu.
Neutral
  1. <body style="background:grey">
  2.  
  3. Odliczanie: <div id="test"></div>
  4.  
  5.  
  6. function startTimer(duration, display) {
  7. var timer = duration
  8. var minutes;
  9. var seconds;
  10. countDown();
  11. var interval = setInterval(countDown, 1000);
  12.  
  13. function countDown() {
  14. minutes = parseInt(timer / 60, 10)
  15. seconds = parseInt(timer % 60, 10);
  16.  
  17. minutes = minutes < 10 ? "0" + minutes : minutes;
  18. seconds = seconds < 10 ? "0" + seconds : seconds;
  19.  
  20. display.innerHTML=(minutes+":"+seconds);
  21.  
  22. if (--timer < 0) {
  23. display.innerHTML=('Tutaj akcja!');
  24. clearInterval(interval);
  25. }
  26. }
  27. }
  28.  
  29. (function() {
  30. startTimer(0.2*60,document.getElementById('test'));
  31. }());
  32.  
  33.  
  34. </body>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.