How to Execute PHP script in 15 seconds? - javascript

I'm building App where I have to get confirmation from user in real time so i have to reload data in 15 seconds and cron job don't works in this period of time.
Any solution in PHP or JavaScript or server side ?

There are actually several ways to achieve this. You can do it with WebSocket and push the data to the browser from PHP, or you can do interval polling from the javascript side.
Resources:
Socket.io
JS Polling

Call ajax inside setInterval, To get data from server in every 15 second and update in DOM
setInterval(function(){
alert("Call ajax here to get data from server in every 15 second and update in DOM");
}, 15000);

Related

Create content which updates asynchronously PHP

I'm trying to create a trading bot with PHP.
I would like to get the value of the currency and update it even without refreshing the page.
In order to do this I should execute this call every 5-10 seconds.
Is that correct?
$summ = $d->getMarketSummary("USDT-BTC");
Is there a way to do this asynchronously? Even if user doesn't reload the whole page.
I've heard of AJAX, but it's Javascript.
Thank you in advance.
You've heard it correctly, you need to do this in JavaScript, with AJAX. There are two parts for this:
1) You need to make an API in PHP, a route that will only respond with the data you want. So a page that when called:
<?php
$summ = $d->getMarketSummary("USDT-BTC");
echo $summ;
?>
And mapped to a url, let's say /data.
2) You need to make a JS in your page that calls that newly created route every-so-often; for that your need to use ajax (xmlhttprequest or Fetch API), and use the setInterval function to call it regularly and update the data in your page accordingly.
If you can't use javascript (AJAX) for this task, your only way is to create a CRON job that fires that PHP script every minute. Unfortunatelly, CRON jobs can't be configured to execute every X seconds, but you can fire it all minutes of the day.

Loading bar with execution time from server

In my website, I use a script (on server) with data sent from the webpage. The problem is that the script execution time (+data transit) takes 5 to 30 seconds to execute and waiting can be quite long for the user so I was wondering what is the best way to create a waiting loading bar? Do I have to execute a first function on the server that will calculate the time, and then send it to the client, or do I use repeted AJAX requests with each time, the current status ?
The advantage with the second one is that it's easier to just send the status after each part of the script than to calculate the speed from just the data sent. Moreover it could be more precise, basing itself on the actual connection time... But I think it could use more server ressources. And if I choose that what is the best delay between each requests ?
Thank you for your answers

Wait 5 seconds before download button appear

I know how to do that with javascript but I need a secure way to do it.
Anybody can view page source, get the link and do not wait 5 seconds.
Is there any solution? I'm working with javascript and django.
Thanks!
The only secure way would be to put the logic on the server that checks the time. Make an Ajax call to the server. If the time is under 5 seconds, do not return the HTML, if it is greater than , than return the html to show.
Other option is to have the link point to your server and if the time is less than five seconds it redirects them to a different page. If it is greater than 5, it will redirect them to the correct content.
Either way, it requires you to keep track of session time on the server and remove it from the client.
Use server side timeout.. whenever there is (AJAX) request from client for download link with timestamp, compare the client sent timestamp with currenttime and derive how much time is required to halt the request at server side to make up ~5 seconds. So by comparing timestamp you can almost achieve accuracy of waiting time as the network delays would be taken into account automatically.
You can use ajax, retrieve the button source code from your back end and intert it on your page.
Something like
$.get('url', function(sourceCode) {
$('#midiv').html(sourceCode);
});

making ajax requests in every 2 seconds is doable?

I have to build a results viewing page where I have to list results of a voting process in live. I am planning to check for DB changes in every 2 seconds ? Is it doable ? will the page crash or get stuck after 1 or 2 hours?
I think you can use settimeout to call the ajax function.
The second better way would be if its possible in your existing architectural setup, use setimeout for making an io connection and then checking for any event , this event is nothing else but a change in the db which is triggered by a node js server.

Need to execute an ajax call incrementally after fixed time periods in javascript?

HI guys, I need to be able to make an ajax call to be made after every few minutes. Basically the ajax call would be to check on new emails in an inbox. If there are new emails it would download the emails to a database. I got all the server side code set up fine. I just need to know how do I set up on the front end the part where the ajax call is made after every few minutes plus it should be set up such that we don't end up with parallel ajax calls being made i.e if the ajax call hasn't returned a response it shouldn't start a new ajax request.
http://www.w3schools.com/jsref/met_win_settimeout.asp
or
http://www.w3schools.com/jsref/met_win_setinterval.asp
set interval is probably more like what you after, you can then call something like
setInterval(function(){ /*code you want to run here */ }, 100);
then your code will run every 100 milliseconds.
You will obviusly need to use a much larger time, something like 60000 for every minute. If you use the settimeout method then you can have it check wait for the response of the ajax, and let the code the handles the ajax response set a new timeout.
There is also Prototype periodical update that makes things a lot easier
few examples
http://www.tutorialspoint.com/prototype/prototype_ajax_periodicalupdater.htm
http://www.prototypejs.org/learn/introduction-to-ajax
http://www.prototypejs.org/api/ajax/periodicalUpdater
I would set up a cookie that bounces back and forth between the server and the client to store the last time an ajax request was made. Then on each page load, a js script reads the cookie and sets a setTimeout for the remaining time before the next ajax request.

Categories