Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop a javascript function from within another function?
    primarykey
    data
    text
    <p>I'm developing a simple slideshow system. I've got the slideshow wrapped in a hidden div, which is shown when a thumbnail from the gallery is clicked.</p> <p>The slideshow works through a function called <code>commence()</code>, which is executed when the play button is clicked.</p> <p>At the moment I've got it set to hide to whole <code>div</code> again when stop is clicked, but I would like to keep the <code>div</code> shown, simply stop the slideshow, in other words, stop the <code>commence()</code> function.</p> <p>Can anyone tell me how to do this? </p> <p>Here is my JS:</p> <pre><code>function commence() { hidden = document.getElementById("hidden"); hidden.style.display = 'block'; pause = document.getElementById("pause"); pause.style.display = 'block'; play = document.getElementById("play"); play.style.display = 'none'; pic = document.getElementById("picbox"); // Assign var pic to the html element. imgs = []; // Assign images as values and indexes to imgs array. /* --------------------------- IMAGE URLS FOR IMGS ARRAY -------------------------*/ imgs[0] = "/snakelane/assets/images/thumb/_1.jpg"; imgs[10] = "/snakelane/assets/images/thumb/_19.jpg"; imgs[1] = "/snakelane/assets/images/thumb/_2.jpg"; imgs[11] = "/snakelane/assets/images/thumb/_20.jpg"; imgs[2] = "/snakelane/assets/images/thumb/_3.jpg"; imgs[12] = "/snakelane/assets/images/thumb/_21.jpg"; imgs[3] = "/snakelane/assets/images/thumb/_4.jpg"; imgs[13] = "/snakelane/assets/images/thumb/_22.jpg"; imgs[4] = "/snakelane/assets/images/thumb/_5.jpg"; imgs[14] = "/snakelane/assets/images/thumb/_23.jpg"; imgs[5] = "/snakelane/assets/images/thumb/_6.jpg"; imgs[15] = "/snakelane/assets/images/thumb/_24.jpg"; imgs[6] = "/snakelane/assets/images/thumb/_7.jpg"; imgs[16] = "/snakelane/assets/images/thumb/_25.jpg"; imgs[7] = "/snakelane/assets/images/thumb/_8.jpg"; imgs[17] = "/snakelane/assets/images/thumb/_26.jpg"; imgs[8] = "/snakelane/assets/images/thumb/_9.jpg"; imgs[18] = "/snakelane/assets/images/thumb/_27.jpg"; imgs[9] = "/snakelane/assets/images/thumb/_10.jpg"; imgs[19] = "/snakelane/assets/images/thumb/_28.jpg"; /* -----------------------------------------------------------------------------------------------------*/ var preload = []; // New array to hold the 'new' images. for(i = 0 ; i &lt; imgs.length; i++) // Loop through imgs array { preload[i] = new Image(); // Loop preload array and declare current index as a new image object. preload[i].src = imgs[i]; // Fill preload array with the images being looped from ims array. } i = 0; // Reset counter to 0. rotate(); // Execute rotate function to create slideshow effect. } // Function to perform change between pictures. function rotate() { pic.src = imgs[i]; // Change html element source to looping images (i === (imgs.length -1))?(i=0) : (i++); // counter equals imgs array length -1. setTimeout( rotate, 4000); // Sets the time between picture changes. (5000 milliseconds). } function init() { [].forEach.call(document.querySelectorAll('.pic'), function(el) { el.addEventListener('click', changeSource); }); function changeSource() { hidden = document.getElementById("hidden"); hidden.style.display = 'block'; newpic = this.src; var pic = document.getElementById("picbox"); pic.src = newpic; } } document.addEventListener("DOMContentLoaded", init, false); function stopSlide() { var hidden = document.getElementById("hidden"); hidden.style.visibility = 'hidden'; pause.style.display = 'none'; var play = document.getElementById("play"); play.style.display = 'block'; } </code></pre> <p>The <code>pause</code> and <code>play</code> statements are not relevant to my question, they simply hide the play button and show the pause button if the slideshow is running, and vice versa.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload