event
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Show Event properties</title>
<style>
table { border-collapse: collapse; }
thead { font-weight: bold; }
td { padding: 2px 10px 2px 10px; }
.odd { background-color: #efdfef; }
.even { background-color: #ffffff; }
</style>
<script>
function showEventProperties(e) {
console.log("entre");
function addCell(row, text) {
var cell = row.insertCell(-1);
cell.appendChild(document.createTextNode(text));
}
var e = e || window.event;
document.getElementById('eventType').innerHTML = e.type;
var table = document.getElementById('tab');
table.innerHTML=""; //reset
var thead = table.createTHead();
var row = thead.insertRow(-1);
var lableList = ['#', 'Property', 'Value'];
var len = lableList.length;
for (var i=0; i<len; i++) {
addCell(row, lableList[i]);
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for (var p in e) {
row = tbody.insertRow(-1);
row.className = (row.rowIndex % 2)? 'odd':'even';
addCell(row, row.rowIndex);
addCell(row, p);
addCell(row, e[p]);
}
document.body.appendChild(table);
}
window.onload = function(event){
//showEventProperties(event);
}
</script>
</head>
<body>
<h1>Properties of the DOM <span id="eventType"></span> Event Object</h1>
<table id="tab"></table>
<script>
var elem = document.getElementById('eventType').parentElement;
//elem.addEventListener("mouseenter", showEventProperties, false);
elem.addEventListener("mouseleave", showEventProperties, false);
//elem.addEventListener("mousemove", showEventProperties, false);
elem.addEventListener("click", showEventProperties, false);
</script>
</body>
</html>
le scroll
Les propriétés scrollTop et scrollLeft peuvent être manipuler
Sous le pont Mirabeau coule La Seine Et nos amours Faut il Qu'il m'en Souvienne
Voici une ref : https://javascript.info/size-and-scroll#scrollleft-scrolltop
Sous le pont Mirabeau coule La Seine Et nos amours Faut il Qu'il m'en Souvienne
bind en action
class GameLoop {
constructor(options) {
this.options = options;
this.running = false;
this.raf = null;
this.delta = 1000;
this.accumulator = 0;
this.last = performance.now();
}
start() {
if (!this.running) {
this.running = true;
this.raf = window.requestAnimationFrame(this.tick.bind(this));
console.log(this.raf);
}
}
stop() {
this.running = false;
window.cancelAnimationFrame(this.raf);
}
tick() {
let now = performance.now();
let dt = now - this.last;
this.last = now;
this.accumulator += dt;
while (this.accumulator >= this.delta) {
if (this.options.render) {
this.options.render();
}
this.accumulator -= this.delta;
}
if (true && this.options.update) {
this.options.update(this.accumulator);
}
if (this.running) {
this.raf = requestAnimationFrame(this.tick.bind(this));
}
}
}
let render = function() {
const posts = [];
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min
for (let i = 0; i < 1000; i++) {
posts.push({
id: `ID_${i}`,
upVotes: random(1, 1000)
});
}
console.time("sort after");
let [c, d] = posts
.reduce((acc, cur) => [cur, ...acc], [])
.sort((a, b) => b.upVotes - a.upVotes)
.slice(0, 2);
console.timeEnd("sort after");
}
update = (v) => console.log(v);
options = {
render,
}
let g = new GameLoop(options);
g.start();
window.setTimeout(g.stop,5000);
Impossible d’arrêter la boucle du jeu ?
trouver la faille !
constructor(options) {
this.options = options;
this.running = false;
this.raf = null;
this.delta = 1000;
this.accumulator = 0;
this.last = performance.now();
}
start() {
if (!this.running) {
this.running = true;
this.raf = window.requestAnimationFrame(this.tick.bind(this));
console.log(this.raf);
}
}
stop() {
this.running = false;
window.cancelAnimationFrame(this.raf);
}
tick() {
let now = performance.now();
let dt = now - this.last;
this.last = now;
this.accumulator += dt;
while (this.accumulator >= this.delta) {
if (this.options.render) {
this.options.render();
}
this.accumulator -= this.delta;
}
if (true && this.options.update) {
this.options.update(this.accumulator);
}
if (this.running) {
this.raf = requestAnimationFrame(this.tick.bind(this));
}
}
}
let render = function() {
const posts = [];
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min
for (let i = 0; i < 1000; i++) {
posts.push({
id: `ID_${i}`,
upVotes: random(1, 1000)
});
}
console.time("sort after");
let [c, d] = posts
.reduce((acc, cur) => [cur, ...acc], [])
.sort((a, b) => b.upVotes - a.upVotes)
.slice(0, 2);
console.timeEnd("sort after");
}
update = (v) => console.log(v);
options = {
render,
}
let g = new GameLoop(options);
g.start();
window.setTimeout(g.stop,5000);
Impossible d’arrêter la boucle du jeu ?
trouver la faille !
Return function en action !
- function baseProperty(key) {
- return function (object) {
- return object == null ? undefined : object[key];
- };
- }
- let length = baseProperty('length');
- console.log(length("denis"));
La closure
En lançant le deboggeur dans VS. on découvre la mise en place d'une closure.
MAX, MIN
const data = [];
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
for (let i = 0; i < 10000; i++) {
data.push({ x: random(1, 1000000) });
}
console.time("sort");
const sortData = data.sort( (a,b) => b.x - a.x);
console.timeEnd("sort");
console.log(sortData.filter(i => i.x === sortData[0].x));
console.time("map");
const mapData = data.map(i => i.x);
const maxData = Math.max(...mapData);
console.timeEnd("map");
console.log(mapData.filter(i => i === maxData));
Inscription à :
Articles (Atom)