javascript - How do I add a pause function to this pageChange timer? -
below in code array of pages shuffled , each of them displayed in iframe amount of time. want able start/stop pagechange
function using button or mouse click. can me this? below working code, or check fiddle: http://jsfiddle.net/xaa1qccm/ (thanks nobe4)
var pages=[]; pages[0]="http://example.com/"; pages[1]="http://www.iana.org/domains/reserved"; pages[2]="http://en.wikipedia.org/wiki/main_page"; pages[3]="http://en.wikipedia.org/wiki/randomness"; var shuffle = function(array){ var shuffledpages = []; while(array.length){ shuffledpages.push(array.splice(math.floor(array.length*math.random()),1)); } return shuffledpages; } var time = 3300; var currentindex = 0; function pagechange() { if(currentindex == 0){ pages = shuffle(pages); console.log(pages); currentindex = pages.length; } currentindex--; document.getelementbyid("frame").src=pages[currentindex]; console.log(currentindex); settimeout(function() { pagechange(); }, time); }; pagechange();
a variable can set determine if rotator running, , setting true or false:
var isrunning = true; .... <button onclick="isrunning = false">stop</button> <button onclick="isrunning = true">start</button>
and check inside method:
function pagechange() { if(isrunning){ ... } settimeout(function() { pagechange(); }, time); };
live example: http://jsfiddle.net/xaa1qccm/1/
Comments
Post a Comment