# Render an spinner

**URL:** <https://discourse.slimframework.com/t/render-an-spinner/2550>\
**Category:** Questions\
**Created:** [July 18, 2018, 3:32pm UTC](https://discourse.slimframework.com/t/render-an-spinner/2550 "2018-07-18T15:32:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xpico](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@xpico](https://discourse.slimframework.com/u/xpico)\
**Post date:** [July 18, 2018, 3:32pm UTC](https://discourse.slimframework.com/t/render-an-spinner/2550/1 "2018-07-18T15:32:56Z")

</div>

Hi,

In my slim app i have a function that it takes a long time. My idea is to show an spinner first and then, when all function finish render my template. Why is the way to do this?

Something like this

```
public function inport($request, $response, $args) {

    // HERE FIRST SHOW THE SPINNER

    // THEN RUN MY CODE (it takes 2min)

    // AND FINALLY RENDER TEMPLATE WITH DATA
    return $this->view->render( $response, 'inportresult.html', $inportresult_data);
  }

```

Any idea?

Thanks,

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [July 18, 2018, 6:39pm UTC](https://discourse.slimframework.com/t/render-an-spinner/2550/2 "2018-07-18T18:39:22Z")

</div>

Hello @xpico,

Instead of one request, you can split it in multiple requests:

Request 1: `/job/start`: starts the job in the background and returns a page with a spinner and some javascript that regularly polls `/job/status` until the job is done. when the job is done, the javascript replaces the spinner with a “completed” message.  
Request 2: `/job/{id}/status` returns the status of the job, e.g. running, done or error

---

<div class="post-metadata">

**Author:** ![xpico](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@xpico](https://discourse.slimframework.com/u/xpico)\
**Post date:** [July 19, 2018, 10:35am UTC](https://discourse.slimframework.com/t/render-an-spinner/2550/3 "2018-07-19T10:35:04Z")

</div>

hi @llvdl,

Thank for your suggestion. You know any method to doing background jobs in easy way?

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [July 21, 2018, 7:41pm UTC](https://discourse.slimframework.com/t/render-an-spinner/2550/4 "2018-07-21T19:41:56Z")

</div>

You can use [exec](http://php.net/manual/en/function.exec.php) to start a another script in the background. If you use linux, you need to redirect the output to another file or stream (e.g. `/dev/null` or a log file) and add an ampersand (`&`) at the end of the command, to make it run in the background.

For example:

```php
<?php
// generate a unique job identifier to monitor the job
$jobId = uniqid();

// run the job script in the background and pass the job id
exec('php job.php ' . escapeshellarg($jobId) . ' >/dev/null &');

print 'Job is started in background with id ' . htmlspecialchars($jobId) . PHP_EOL;

```

_job\_start.php_

```php
<?php
$jobId = $argv[1]; // get job id passed through command line

sleep(10); // do some heavy processing...

// job is finished, store this information in a file or database, so status can be polled.
$fh = fopen('finished.txt', 'a');
fputs($fh, $jobId . PHP_EOL);
fclose($fh);

```

_job.php_

---

<div class="post-metadata">

**Author:** ![marcelloh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/marcelloh/32/404_2.png) [@marcelloh](https://discourse.slimframework.com/u/marcelloh)\
**Post date:** [July 23, 2018, 2:31pm UTC](https://discourse.slimframework.com/t/render-an-spinner/2550/5 "2018-07-23T14:31:51Z")

</div>

I would go to a page and inform the user about a longer running progress,  
after a small period of time, then redirect to the long process and do it’s job.  
As a result, render some statistics or something.
