Saturday 15 February 2014

javascript - Pause & Resume setTimeout in a loop -



javascript - Pause & Resume setTimeout in a loop -

i have next $.each loops through object. every 5 seconds get_file() function called. there anyway include pause , resume option. if click pause loading of info stops. , of course of study when click resume button starts 1 time again left off.

hope can help.

$.each(obj, function (i, v) { settimeout(function () { file = v.new_file; var timeline_date = moment(v.last_modified_date).format("dddd, mmmm yyyy, h:mm:ss a"); bhover = 0; //re-highlight links $(".timeline_msg").html(timeline_date); get_file(file); }, 5000 * (i + 1)); });

make array of timers, can clear them when pause button clicked. utilize global variable maintain track of how far through list of files load. can resume left off.

var curfile = 0; var load_file_timers; function load_files() { load_file_timers = obj.map(function(v, i) { if (i < curfile) { homecoming null; } else { homecoming settimeout(function() { var file = v.newfile; var timeline_date = moment(v.last_modified_date).format("dddd, mmmm yyyy, h:mm:ss a"); bhover = 0; //re-highlight links $(".timeline_msg").html(timeline_date); get_file(file); curfile++; }, 5000 * (i - curfile + 1)); } }); } load_files(); $("#pause").click(function() { $.each(load_file_timers, function(i, timer) { if (timer) { cleartimeout(timer); } }); }); $("#resume").click(load_files);

javascript jquery

No comments:

Post a Comment