Mouseenter firing multiple times
I'm trying to get a random image to appear on each new hover of a div. The images are coming from some JSON data. It's currently working, but the mouseenter function is firing when the mouse is still inside the div. Maybe the getJSON request is working each time the image is delivered? Is it possible to store the JSON data in an array, and access that array each time mouse enters? Any advice would be much appreciated :)
My jQuery code looks like this:
- $('#imagehover').on('mouseenter', function() {
- $.getJSON('images.json', function(result) {
- var index = Math.floor(result.url.length * Math.random());
- $("#imagetestresult").html($('<img>', {
- src : result.url[index]
- }));
- });
- });
The JSON data looks like this:
- {
- "url": [
- "http:\/\/image1.jpg",
- "http:\/\/image2.jpg",
- "http:\/\/image3.jpg",
- "http:\/\/image4.jpg"
- ]
- }
HTML:
- <div id="imagehover">Images
- <div id="imagetestresult" class="random-image"></div>
- </div>