-1

I created this function on JSFIDDLE , and copied it to a html file like this,

<!DOCTYPE html>
<html>

<img src="https://img.clipartfest.com/bdaa48cee45dde7a22d715ece7f549c0_free-man-flying-a-rocket-rocket-man-clipart_2000-1778.png" height="82" width="82"
id="friends"/>

<style>#friends { position: absolute; }</style>

<script language="JavaScript" type="text/javascript" >var t = 0;
  var width = window.innerWidth
  || document.documentElement.clientWidth
  || document.body.clientWidth;
  function moveit() {
     t += 0.01;

var r = 100;
var xcenter = 100;
var ycenter = 100;
var newLeft = (Math.floor(xcenter + ( 1*r*Math.tan(t))));
var newTop = (Math.floor(ycenter + (r*Math.sin(t))));

if(newLeft < xcenter && newTop<r ){newTop=2*r-newTop}
if(newLeft > xcenter && newTop>r ){newTop=2*r-newTop}
$('#friends').animate({
    top: newTop,
    left: newLeft,
}, 1, function() {
    moveit();
});
}


moveit();

</script>



</html>

It gives me lot of errors . but works perfectly in JSFIDDLE , Please tell me what I'm doing wrong . I'm new to JS.

user8006503
  • 31
  • 2
  • 6

2 Answers2

4

The example uses jQuery version 1.6. Try including that in your page.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

Update: And disable the extension Imacros for Chrome as your exception comes from there.

Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
  • Error in event handler for (unknown): TypeError: Cannot read property 'state' of null at CSRecorder.onQueryStateCompleted (chrome-extension://cplklnmnlbnpmjogncfgfijoopmnlemp/content_scripts/recorder.js:106:17) – user8006503 May 22 '17 at 14:20
  • still getting this error – user8006503 May 22 '17 at 14:20
  • That's a Chrome extension error... Try to disable the extension. Probably iMacros for Chrome from what Google suggests me. Anyhow, I cannot help you with that error. – Nico Van Belle May 22 '17 at 14:22
2

You're using jQuery functions without importing the library, try adding

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

at the top of your html

Jonas Zell
  • 106
  • 1
  • 4