0

4I'm currently working on a HTML code for a presentation that changes image based on a javascript array.

I managed to incorporate a looping code from a friend into my code, but I don't understand how to only make the image change once.

At the moment, it works that the javascript loops the items in the array to display different images, but I'd like it to work that it only changes once, based on a button click.

I'd like to keep my array if possible. Here's the code:

<html>

<head>
  <script type='text/javascript'>
    var banners = ["banner1.png", "banner2.png", "banner3.png"
      "banner4.png"
    ];
    var bnrCntr = 0;
    var timer;

    function banCycle() {
      if (++bnrCntr == 4)
        bnrCntr = 0;

      timer = setTimeout("banCycle()", 4000);
    }

    function stopCycle() {
      clearTimeout(timer);
    }
  </script>
</head>

<body>
  <img src="bannerad1.png" name="banner" width=110 height=200>

  <form>
    <input type="button" value="Cycle" name="Cycle" onclick="banCycle()">
    <input type="button" value="Stop" name="Stop" onclick="stopCycle()">
  </form>
</body>

</html>

Thank you in advance!

EDIT- Just to clarify, I'm limited to pure javascript/HTML, and therefore I cannot use JSQuery.

4
  • remove timer = setTimeout("banCycle()",1000);? Commented Dec 7, 2015 at 19:46
  • Sounds like you want a button to advance to the next image? And doesn't cycle through the remainder of the array? Commented Dec 7, 2015 at 19:48
  • @Fabricator It didn't work, and anyway, I'm looking for it to work on a button click, removing the timeout for the loop is still going to make it loop. Commented Dec 7, 2015 at 19:49
  • @EnigmaRM Yes, that's correct. Commented Dec 7, 2015 at 19:50

2 Answers 2

1

The basic idea can be seen here in this JSFIDDLE

HTML

<img id="banner" src="http://placehold.it/350x150">
<button onclick="next()">
  Next Image
</button>

JavaScript

var banners = ["http://placehold.it/350x150", "http://placehold.it/400x200", "http://placehold.it/300x100"]
var counter = 1;

function next() {
  if (counter >= banners.length){
    counter=0;
  }
  document.images.banner.src = banners[counter];
  counter++;
}

What you want to do is remove all of your cycle stuff. You can keep your Counter (what you have named bnrCntr). All you have to do is increment the counter by one each time the button is clicked.

By this logic, it would also be very easy to implement a "back" button that decrements the counter

Sign up to request clarification or add additional context in comments.

Comments

1

You can make use of .one() event in jquery. It would trigger the click event only once.

<script>
  $().ready(function(){
    var banners = ["banner1.png","banner2.png","banner3.png""banner4.png"];
    var bnrCntr = 0;
    var timer;
    $('input:button[name=Cycle]').one("click",function()
    {
      if(++bnrCntr == 4)
      bnrCntr = 0;

      document.images.banner.src = banners[bnrCntr];

      timer = setTimeout("banCycle()",1000);
    });

    $('input:button[name=Stop]').one("click",function()
    {
      clearTimeout(timer);
    });
});
    </script>

For a non jquery ( pure javascript ), just do a "return false". It would prevent further processing.

var banners = ["banner1.png","banner2.png","banner3.png""banner4.png"];
var bnrCntr = 0;
var timer;
var executedBanCycle = false;
var executedStopCycle = false;

function banCycle(e)
{
  if(executedBanCycle)
      return false;

  executedBanCycle = true;
  alert("Execute BanCyle");
  if(++bnrCntr == 4)
  bnrCntr = 0;

  document.images.banner.src = banners[bnrCntr];

  timer = setTimeout("banCycle()",1000);       

}

function stopCycle(e)
{
    if(executedStopCycle)
       return false;  
  executedStopCycle = true;
  alert("Execute stopCyle");
   clearTimeout(timer);

}

Example : http://jsfiddle.net/f5xn7zhk/

1 Comment

That sounds great! However, I'm currently limited to pure javascript/HTML coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.