Description

Install device API
I have an API to install the device. When I click, the API will start installing my device and return the taskID

Monitor API
Then I will use taskID to pass to another API call to track the installation progress.
The monitor API will return an integer from 1 to 200, which is a percentage of my progress in my installation.
My goal
keep calling the monitor APIs and asynchronously update my progress bar in real time. When it reaches 200, it will be done, I will hide the progress bar and show a success message.

I tried
logics
- Call API
- Wait 1 s
- Call the API again if it has not reached 200
- Reiteration
- Until I got 200 percent
- Then get out of cycles
- the end
core code

The code
var ajax = $.ajax({ headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value')}, url: '/installDevice', type: 'POST', dataType: 'json', data: {'security-level':'medium'} }); ajax.done(function ($installDeviceResponse) { console.log($installDeviceResponse); if($installDeviceResponse['result'][0]['status']['code'] == 0){ var taskId = $installDeviceResponse['result'][0]['data']['task']; var $percent = 0; do { $.ajax({url: '/monitor/'+taskId}) .done(function ($percent) { setTimeout(function(){ console.log('P TaskIdResponse : ',$percent); // update prograssbar // I don't have this yet. // I'm trying to print it out for now }, 1000); }); } while ($percent < 200); }else{ var message = $installDeviceResponse['result'][0]['status']['message']; var code = $installDeviceResponse['result'][0]['status']['code']; console.error('Error code : ' + code + ' ' + message ); } }); return;
I set the timer 1s because I do not want the DDOS server API.
Result
As a result, I got an endless loop.
I donβt have a progress bar yet, because I want the code to work in the console first. All I got now is the download icon.
The download icon seems to freeze.

The console seems to freeze, cannot even extend 1 of my answer.

The computer is very noisy due to the high processor load. Chrome is slow to respond.
How can I debug this?
javascript jquery ajax php laravel
kyo
source share