- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试制作一个新闻代码指令,该指令将元素滑过其父元素,以便您可以在字符串太长时读取它。我被卡住了,因为我无法让 setInterval()
函数在其当前范围内调用 moveLeft()
函数,从而移动目标元素(this.node1
) 超过 1px
。如果我将元素、函数和参数传递到 setInterval()
中,则不会调整 DOM,因为我认为该元素超出了范围。 如何从 setInterval()
函数调用其当前作用域中的 moveLeft()
函数?该函数在外部调用时工作正常setInterval()
函数。
import { Directive, ElementRef, HostListener, Input, Renderer, OnInit } from '@angular/core';
@Directive({ selector: '[ticker]' })
export class TickerDirective implements OnInit {
currMargin: number; // i.e.: 4
newMargin: string; // i.e.: '4px'
currInterval: any; // timeout function variable used to kill the timeout
container: any; // container element
node1: any;
node2: any;
tickerNodes: any[];
textValue: string;
@Input('text') text: string; // display string passed in on an attribute
constructor(private el: ElementRef, private renderer: Renderer) { }
@HostListener('mouseenter') onMouseEnter() {
// this is where the script is failing
this.currInterval = setInterval( this.moveLeft(), 100);
}
@HostListener('mouseleave') onMouseLeave() {
clearInterval(this.currInterval);
this.currMargin = 0;
}
moveLeft() { // slide the elements to the left
this.currMargin -= 1;
this.newMargin = this.currMargin + 'px';
this.renderer.setElementStyle(this.node1, 'margin-left', this.newMargin);
}
ngOnInit() { // instantiate the elements
this.currMargin = 0;
this.textValue = this.el.nativeElement.attributes[2].nodeValue; // sets the text value passed from the component to the directive through an attribute
// build the container
this.container = this.renderer.createElement(this.el.nativeElement, 'div');
this.renderer.setElementClass(this.container, 'ticker-container', true);
// create the left most element
this.node1 = this.renderer.createElement(this.container, 'div');
this.renderer.setElementClass(this.node1, 'ticker-node', true);
this.renderer.setText(this.node1, this.el.nativeElement.attributes[2].nodeValue);
// create the right most element
this.node2 = this.renderer.createElement(this.container, 'div');
this.renderer.setElementClass(this.node2, 'ticker-node', true);
this.renderer.setText(this.node2, this.el.nativeElement.attributes[2].nodeValue);
// render the elementss
this.tickerNodes = [this.node1, this.node2];
this.renderer.attachViewAfter(this.container, this.tickerNodes);
}
}
最佳答案
首先,在您的原始版本中,您在间隔设置期间执行 moveleft,而不是提供函数。因此它会在您设置时执行,但不会再执行。
即。 setInterval( this.moveLeft, 100)
而不是 setInterval( this.moveLeft(), 100)
但真正的问题是为 setInterval 提供箭头函数,而不是直接提供函数,因为简单地给它函数将改变 this
的上下文(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
例如。 setInterval( () => { this.moveLeft(); }, 100)
关于angular - setInterval() 问题不访问 Angular2 中的指令函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40517662/
这个问题已经有答案了: JavaScript closure inside loops – simple practical example (45 个回答) 已关闭 8 年前。 我试图创建多个 se
这是我的情况,我需要加快函数运行时间,所以 setInterval 不是一个明智的选择,对吧?因为每次至少要花费 4 毫秒。 所以,我可以将 setInterval 函数更改为 requestAnim
我正在尝试下面的代码,看看是否可以将 setInterval Id 存储为关联数组中唯一键的值,然后通过使用唯一值作为键来停止被调用函数中的间隔,如下所示我将 setInterval Id 作为它的值
我花了很长时间试图弄清楚如何使用具有 CSS 样式缓动功能的 Google Maps API 为折线上的符号设置动画。我让它工作 with this Gist和 this Google Maps AP
我是这里的 JS 新手,正在研究一个在给定时间段后开始的倒数计时器。基本上,当用户单击按钮时时钟开始,第二个时钟在第一个计时器用完时开始。我知道“setInterval”是完成这个的最好方法。我面临的
嗨, friend 们,我想只使用一个 setInterval 函数来运行我的代码。目前我正在使用两个 setInterval's 来实现我的目标,我们可以只使用一个 'setInterval' 来获
我的“setInterval”函数有问题,我想在将鼠标悬停在 div 上时启动该函数,但是 setInterval 在我将鼠标悬停在 div 上时第一次起作用=>如果我停留在div,它不会继续改变图片
我想展示两次:一次在你的国家,一次在日本用JS 问题是第二个 setInterval 停止了第一个,我不知道如何进行两次运行。 完整代码 In your region:
好吧,这个问题有几个问题。 首先,我要求 setTimeout() 和 setInterval() 我见过几种不同的调用方式,我想知道哪种方式最适合这种情况。 我正在制作一个 js/canvas 游戏
setInterval(function () { //======= //code //======== if(--timer&etype="; } },1000); 这里定时器结束后,而不是重定
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我的前老板有一个 weird bug where when he used setInterval with a long delay interval : setInterval(func, 300
这个问题已经有答案了: How does the "this" keyword work, and when should it be used? (22 个回答) How to access the
我的印象是 setInterval("/*some code*/", time) 相当于 setInterval(function() { /*some code*/ }, time) 显然不是
我对 JavaScript 和 NodeJS 非常陌生,我只是想了解 NodeJS 中的发射器模式。当我尝试使用 setInterval 函数每秒发出一个刻度事件时,程序似乎工作正常:-
我有一个简单的 setTimeout 函数,它在特定时间运行并且工作正常: var now = new Date(); var milliTillExec = new Date(now.getFull
在本教程中,您将借助示例了解 JavaScript setInterval() 方法。 在 JavaScript 中,可以在指定的时间间隔内执行一段代码。这些时间间隔称为定时事件。 有
Js: function cometConnect(){ $.ajax({ cache:false, type:"post", d
LE2。关于如何解决此问题的任何其他想法? 我有这段代码,但不知道为什么不能正常工作: $(function autorun() { if ($("#contactForm").is(":visibl
这个问题在这里已经有了答案: How to make a setInterval stop after some time or after a number of actions? (6 个答案)
我是一名优秀的程序员,十分优秀!