After multiple rounds of editing this post, I keep uncovering new information through testing and debugging. Here is my current situation:
I have a method thinker
that takes a significant amount of time to process. To address this, I have decided to call it via Ajax and display an indicator to the user, which works perfectly.
Additionally, I want to show the user the progress of the "thinking" process, either in percentage or ticks.
This task is not too challenging as the thinker
function has a loop that saves new thoughts
to the database in each iteration.
Essentially, the counter
displays the number of "thoughts" associated with a specific ID.
The issue arises when the callback option fails to trigger.
To simplify, the callback only executes twice - before the request (displaying zero) and after the thinker
completes its task (showing the full count).
Here is the basic code:
<span id="indicator" style="display: none;"><img src="/img/ajax-loader.gif" alt="" /></span>
<span id="progress"></span>
<span id="clicker" style="cursor: pointer;">Do it!</span>
<script type="text/javascript">
//<![CDATA[
Event.observe('clicker', 'click', function(event) {
new Ajax.Request(
'/examples/thinker/123',
{asynchronous:true, evalScripts:true, onComplete:function(request, json) {Element.hide('indicator');}, onLoading:function(request) {Element.show('indicator');}}
);
new Ajax.PeriodicalUpdater(
'progress',
'/examples/counter/123',
{asynchronous:true, evalScripts:true, frequency: 2}
);
});
//]]>
</script>
UPDATE:
Using the Firebug XHR console, I delved deeper into this issue. I even tried using jQuery for testing, but it did not resolve the problem.
It seems the problem lies with CakePHP. When I replicated the scenario with basic PHP, everything worked simultaneously. However, with CakePHP, the counter
is called first, followed by the thinker
, and then the counter
is updated every two seconds but only responds after the thinker
completes.
Could it be that CakePHP cannot handle the second request until the first is finished?
Best regards, Paul