How to get Session with javascript while loading

+2 votes

Here is my scenario:

  • I'm processing an excel file that'll took about ~10 minute
  • While processing, I want to send a feedback to user that we are currently processing

My idea was using Session that will put value while processing, and get that Session using javascript in current view
Here's my script:

<script type="text/javascript">
    $(document).ready(function() {
        var element = document.getElementById("progress");
        setInterval(
            function(){
                element.innerHTML = "{{Session::get('progress')}}";
            },500
        );
    });
</script>


And somewhere in my controller, let's just say like this:

    $i = 0;
    while(!$done){
        processingComplicated();
        Session::put('progress', $i);
        Session::save();
    }


And my basic view :

<div id="progress">0</div>

Basically, I want the current page to get the Session data and update the view(id="progress"), but it won't change.
Can it be done? \

Thanks!!

Jun 11, 2020 in Java-Script by kartik
• 37,520 points
4,104 views

1 answer to this question.

0 votes

Hello @kartik,

Use AJAX to update the view:

$(document).ready(function() {
    var element = document.getElementById("progress");
    setInterval(
        function(){
            $.get( "processing-status", function( data ) {
                 element.innerHTML = data;
            });
        },500
    );
});

And in your routes, controller or wherever you want

Route::get('/processing-status', function()
{
    return Session::get('progress');
});

Hope it works!!

Thank you!

answered Jun 11, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How to get the browser to navigate to URL in JavaScript?

Hii, This works in all browsers: window.location.href = '...'; If ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,840 points
2,672 views
0 votes
2 answers

How can I set a session var using javascript and get it via php code?

Yes it is possible. You can either ...READ MORE

answered Aug 9, 2020 in Java-Script by Okugbe
• 280 points
28,778 views
0 votes
1 answer

How to Store PHP variable with HTML in JavaScript in Laravel Blade Template?

Hello @kartik, The double curly brackets {{ }} will always ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,840 points
17,294 views
0 votes
3 answers
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,145 views
+1 vote
1 answer

What is css box module?

Hey, All the element present in html follows ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,206 views
0 votes
3 answers

Explain the difference between visibility:hidden; and display:none?

display:none means that the tag in question will ...READ MORE

answered Dec 14, 2020 in Web Development by Gitika
• 65,770 points
119,705 views
+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 2,971 views
0 votes
1 answer

How to get the current URL with JavaScript?

Hello @kartik, Use: window.location.href As noted in the comments, ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
1,285 views
0 votes
1 answer

How to access GET directly from JavaScript?

Hello @kartik, You can first go through: window.location.search It will ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,840 points
2,402 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP