开发者

Multifunctioning in Javascript (jQuery)

The concept is running multiple functions concurrently.

The reason is, I have a page which performs various actions through ajax. These actions includes making multiple backups of new files uploaded in the upload directory. But I want this process to be initiated by a moderator.

As this is a very lengthy process(might even take hours to complete), it blocks others ajax requests from executing, until this process complete.

I want to execute functions along with the previously executed function parallelly.

I am using jQuery's Ajax to sent initiate the request.

I will try to explain using a example


I use ajax request to perform a server side action. These requests are initiation on click event of a anchor text.

$(".ajax").live('click',function() {
     action = $(this).attr['rel'];
     $.post(
         'proccess.php',
         { action: action }, //among many action one is for the backup
         function(data) {
            //once a particular action is complete check the data to decide if it requires to continue
            flag = data.flag;
            // if the action fails or if action is backup then the flag is true
            if(flag) {
                //reexecute the same function
                $(this).trigger('click');
            }
         },'json'
     );
});

In the case if action=='Backup', it backups one file at a time then responds to the script with the json with a true flag and success message to notify about the progress and reexecutes the same function.

Where as other actions will include actions to view the backup log, errors, and other actions that are not even related backup.

So once I click the backup option to initiate the backup progress, I cannot execute other actions

But I am using trigger to initiate another request because if multiple function trigger is pending and the function has finished backing up one file then before sending another call of same function it executes the pending function execution first.


But I need a better soluti开发者_JAVA技巧on, and for that I need to execute multiple instances of a same function many times.


Sounds like this would be better designed with the concurrency on the server side. Have your ajax request start the backup, and immediately return (with like a "backup process started" message)

You can then query the server every minute or so to see how the process is going. That way each ajax request will be over in milliseconds, freeing the client up to do whatever else you need.


Educated guess: you are using PHP in the server backend and your PHP are using sessions. If so, you should read the necessary session data and issue a call to session_write_close() as soon as possible. That way, the session file is no longer locked and can be used by other concurrent calls.

Update: If you say that question is not related to AJAX, then the answer is: you cannot.

However, if strictly necessary you may be able to simulate the feature playing with DOM and event handlers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜