var moves = [];
leftpx = 0;
downpx = 0;
uppx = 0;
rightpx = 0;
var up = function() {
moves.push('up')
}
var down = function() {
moves.push('down')
}
var left = function() {
moves.push('left')
}
var right = function() {
moves.push('right')
}
document.addEventListener("moveRight", function(e) {
console.log(e.detail); // Prints "Example of an event"
// document.getElementById('square').style.left =
myMove4(100);
});
document.addEventListener("stopRight", function(e) {
console.log(e.detail);
clearInterval(e.detail);
// Prints "Example of an event"
// document.getElementById('square').style.left =
//myMove4(100);
});
var Run = function() {
// Add an event listener
// document.addEventListener("name-of-event", function(e) {
// console.log(e.detail); // Prints "Example of an event"
// });
// Create the event
for (var i = 0; i < moves.length; i++) {
if (moves[i] == 'up') {
document.getElementById('square').style.top = setInterval(myMove(), 3000);
};
if (moves[i] == 'left') {
document.getElementById('square').style.left = myMove3()
};
if (moves[i] == 'down') {
document.getElementById('square').style.top = myMove2()
};
if (moves[i] == 'right') {
// rightpx = rightpx + 100;
// Dispatch/Trigger/Fire the event
if (r) return;
var r= setInterval(function(){
var event = new CustomEvent("moveRight", { "detail": "Example of an event" });
document.dispatchEvent(event)
var event1 = new CustomEvent("stopRight", { "detail": r });
document.dispatchEvent(event1);},300);
// document.getElementById('square').style.left = setInterval(myMove4(100),3000);
};
}
moves = [];
}
function myMove() {
var elem = document.getElementById("square");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos == -100) {
clearInterval(id);
} else {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.