JavaScript How do I create several shrek images in a row?

So, I have a page, where when a user holds left mouse button, Shrek images are drawn where his/her cursor is. The problem is, only one picture is created when the mouse is down, but I need an ENDLESS CURRENT OF SHREKS.

Here is the code:

var shrekId = 0;

document.onmousemove = function(event) {
  mouseX = event.clientX;
  mouseY = event.clientY;
}

window.addEventListener('selectstart', function(e){ e.preventDefault(); });

document.body.onmousedown = function(event) {
  shrekId = 0;
  interval = setInterval(draw(event), 100);
}

document.body.onmouseup = function() {
  clearInterval(interval);
}

function draw(event) {
  this["img" + shrekId] = document.createElement("img");
  this["img" + shrekId].src = "http://pngimg.com/uploads/shrek/shrek_PNG3.png";
  this["img" + shrekId].style = "height: 100px; width: 100px; position: absolute; left: " + (mouseX - 50) + "px; top: " + (mouseY - 50) + "px; x-index: 100;";
  this["img" + shrekId].ondragstart = function() { return false; };
  document.body.appendChild(this["img" + shrekId]);
  shrekId += 1
}
Answer:1

setInterval() takes a function as a parameter, this is done by passing the function name (without the brackets) like so:

myFunc()
{
    console.log('hello');
    return 1;
}
setInterval(myFunc, 100);

When you do include the brackets (like you did in your example), it will run the function, take the return of that function, and provide that to the setInterval instead. For example:

myFunc()
{
    console.log('hello');
    return 1;
}
setInterval(myFunc(), 100);

turns into

setInterval(1, 100);

because myFunc() returns 1.

So you should be able to fix your code by removing the brackets from your interval:

interval = setInterval(draw(event), 100);
// to
interval = setInterval(draw, 100);

Which also means you have to remove the parameter from the function:

function draw(event) {
// to
function draw() {
Answer:2

The problem is with this line: interval = setInterval(draw(event), 100);

The first parameter of setInterval should be a function, but you're calling the function here and passing in the result, try the following instead:

interval = setInterval(() => { draw(event) }, 100);

Or if you aren't supporting ES6:

interval = setInterval(function() { draw(event) }, 100);

It doesn't look like you're using event inside of the draw function though, if that's the case you can just pass the draw function itself:

interval = setInterval(draw, 100);

Answer:3

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

  1. formatting json string
  2. formatting json string in java
  3. formatting json string c#
  4. formatting json string js
  5. python string formatting json
  6. javascript formatting json string

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

  1. facebook game invite
  2. facebook game invite friends
  3. facebook instant game invite
  4. facebook instant game invite friend

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

  1. discord force user into voice channel

I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...

I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...

  1. function chaining with return
  2. function chaining in javascript
  3. function chaining in c++
  4. function chaining in python
  5. function chaining in java
  6. function chaining in jquery
  7. function chaining in node js
  8. function chaining in php
  9. function chaining in java 8
  10. function chaining in js
  11. function chaining in scala
  12. function chaining in rust
  13. distributed function chaining with anycast routing