How to create image animation in javascript

How to create image animation in javascript



I am trying to create an image animation which starts by clicking on Start Animation button and should alternate with another picture every half a second until I click on Stop Animation button. When I run this code, it does not output anything. what is wrong with the following code ?


Start Animation


Stop Animation



My coding:


<button onClick="StartAnimation()">
Start Animation
</button>

<button onClick="StopAnimation()">
Stop Animation
</button>

<script>
var counter;
var Schedule;
function StartAnimation()
var images = document.getElementById("fish");
images.src="https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";


Schedule = setInterval(animationfunction,5000);


function animationfunction()
var fish_img = document.getElementById("fish");
fish_img.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png";

function StopAnimation()
clearInterval(Schedule);






2 Answers
2



You've misnamed your function function StarAnimation(){ should be function StartAnimation(){


function StarAnimation(){


function StartAnimation(){






I have corrected it but still not working

– muzzi
Sep 15 '18 at 16:10






You need to define where in the HTML the image is. you can see it working here: codepen.io/dotEthan/pen/xaJvXx

– Ethan Strauss
Sep 15 '18 at 16:20






Yes, It is working in the link but the image should not be shown initially and image is not alternating. It just changes once and stops.

– muzzi
Sep 15 '18 at 16:30







Whoops, just deleted that pen, will recreate appropriately. hold on...

– Ethan Strauss
Sep 15 '18 at 16:38






Thanks @Ethan but I dont want it to change the happy fish to sad fish only once. It must be changing every half a second means after every 0.5 second image will be changed. Sad Fish will keep on changing to Happy and Happy will change to sad every half second until I click stop.

– muzzi
Sep 16 '18 at 0:53




Try to add this function.
Your code looks like, you only want to change your image once. Try to add loop or you can complete in one if-else statement like this.


if-else


function animationfunction()

if(image == "happy")
image = "sad";
fish_img.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png";
else
image="happy";
fish_img.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";


function StopAnimation()
clearInterval(Schedule);



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế