Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1
- class SimpleCollapse {
- constructor(item) {
- // Инициализируем необходимые элементы и состояние
- this.item = document.querySelector(item);
- this.control = this.item.querySelector('.collapse-control');
- this.content = this.item.querySelector('.collapse-content');
- this.collapseState = false;
- }
- toggleCollapseState() {
- // меняем состояние на противоположное
- this.collapseState = !this.collapseState;
- }
- toggleCollapseContent() {
- // в зависимости от состояния прячем/показываем элемент
- this.content.style.display = this.collapseState ? 'none' : 'block';
- }
- init() {
- this.control.addEventListener('click', e => {
- // При клике
- e.preventDefault();
- this.toggleCollapseState();
- this.toggleCollapseContent();
- });
- }
- }
- // =========================================
- // 1a
- import $ from 'jquery'; // подключаем jquery
- import SimpleCollapse from './SimpleCollapse'; // подключаем SimpleCollapse
- class JqueryCollapse extends SimpleCollapse {
- constructor(item) {
- super(item);
- }
- toggleCollapseContent() {
- // меняем скрытие/показ элемента на анимированный из jquery
- $(this.content).stop().slideToggle();
- }
- }
- // ============================
- // 2 На каллбеках
- let a = undefined;
- let b = undefined;
- setTimeout(
- (function(callback) {
- a = 'a';
- callback();
- })(function() {
- setTimeout(function() {
- b = 'b';
- console.log(a);
- console.log(b);
- }, 1000 * Math.random());
- }),
- 1000 * Math.random()
- );
- // ==================================
- // 2. На промисах
- let a = undefined;
- let b = undefined;
- Promise.all([
- new Promise(resolve =>
- setTimeout(() => {
- a = 'a';
- resolve(a);
- }, 1000 * Math.random())
- ),
- new Promise(resolve =>
- setTimeout(() => {
- b = 'b';
- resolve(b);
- }, 1000 * Math.random())
- ),
- ]).then(([a, b]) => {
- console.log(a);
- console.log(b);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement