jquery - Abort Interval when states finished Javascript -
I am trying to terminate my interval when all the states received from the requests are in the state called "success" i interval It has been shut down correctly but it seems that they have been discontinued very soon, I am not a Javascript expert, so there is a problem with my code, if anyone is looking at the $ request received and "Success calculation Is there a synchronization problem between "?
var tid = setInterval (update_status, 2000); Function update_status () {var task_table = $ ("# task_table"); Var Task_table_roose = $ (Task_table) .find ('tr'); Var work_table_ length = Task_table_rov.label; Var id_table = []; Var state_ counter = 0; // Find the ID of the view table and stock them for id_table (var i = 0; i & lt; function_ table_rows.length; i ++) {id_table.push (task_table_rows [i] .id); } // Request and update the term for the job (var i = 1; i & lt; id_table.length; i ++) {state_counter = change_state (i, task_ table_rows [i], state_count); } // Calculate the maximum number of jobs that can be created in the state "success" max_number_success = id_table.length - 1; // If the number of "success" is the number of work, then close the interval if (state_counter == max_number_success) {abortTimer (); } Function change_state (i, task_table_row, state_counter) {$ .get ("/ task / id =" + id_table [i]) done (function (data) {task_status = data ["task"] ["status"]; / / Change HTML class here and other stuff ...}); If (Task_Status == 2) {Return (state_con + 1); } And {return state_ counter; }} Function abortTimer () {clear interval (TID); }}
Thanks.
You need to change it the way you think about the problem as you call AJAX You are not coming back from now. Instead you must provide the callback mechanism to execute when the response is ready. Take a look at this revised version of your code:
// Calculate the maximum number of tasks that can be created in the state "success" max_number_success = id_table.length - 1; // Request and update work for the job. (Var i = 1; i & lt; id_table.length; i ++) {change_state (i, task_ table_rows [i], state_counter, function (count) {state_counter + = count; // If the number of "success" works Number, then stop interval (if state_counter == max_number_success) {abortTimer ();}}); } (Function (data) {task_status = data ["task"] ["status"]; callback (work_status == 2? 1: 0);}); }
Note how you use the change_state
function in the callback
parameter, and how do you change_state Loop.
Comments
Post a Comment