我有一系列颜色代码:
array = %w(#646C74 #F68848 #1FA45C etc...)
Run Code Online (Sandbox Code Playgroud)
每次我查询数组时,我都会与最后使用的颜色进行比较,并希望获得下一个颜色.如果使用的最后一个是数组中的最后一个,我想要第一个.
是否有更专注或简洁的方法来解决这个问题:
index_of_last_used_color = array.index(last_used_color)
next_position = index_of_last_used_color + 1
if color = array[next_position]
return color
else
return array.first
end
Run Code Online (Sandbox Code Playgroud) 我有一个项目列表,我想在三列中按字母顺序(从上到下)均匀显示.是否可以仅使用css进行管理?
使用下面的示例代码,三列将包含以下数量的项目:3/3/2.
<div class="content">
<div class="list_container">
<div class="list_item">Adis Abeba</div>
<div class="list_item">Amsterdam</div>
<div class="list_item">Beijing</div>
<div class="list_item">Buenos Aires</div>
<div class="list_item">Johannesburg</div>
<div class="list_item">Mexico City</div>
<div class="list_item">Paris</div>
<div class="list_item">Stockholm</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当我检测到已更改且即将更新时,我尝试更改attribute_1值。attribute_2
对象已更新,但设置的attribute_1值并未持续存在,为什么?
before_update :check_attribute2_change
def check_attribute_2_change
if attribute_2_changed?
attribute_1 = nil
end
end
Run Code Online (Sandbox Code Playgroud) <button disabled={this.state.passingValidtions} type='submit'>Save</button>
Run Code Online (Sandbox Code Playgroud)
当上面的内容button被禁用时,我希望onClick触发,这样我就可以向用户显示一条消息,说明为什么表单不可提交。
但残疾人也button有残疾。onClick
这里有什么解决方法,以便我可以将其button置于禁用模式并仍然触发onClick吗?
我有一个每 4 秒更改一次图像的组件,如下所示:
state = {
images: this.props.items,
imageIndex: 0,
}
componentDidMount() {
this.interval = setInterval(() => this.changeImage(), 4000)
}
changeImage = () => {
const { imageIndex, images } = this.state
if (imageIndex === images.length - 1) {
this.setState({ imageIndex: 0 })
} else {
this.setState({ imageIndex: imageIndex + 1 })
}
}
render() {
const { images, imageIndex } = this.state
return <img src={images[imageIndex]} />
}
Run Code Online (Sandbox Code Playgroud)
该组件在页面上的 6 个位置使用。
问题是几分钟后,风扇继续运转,计算机变热。我不知道这是由于CPU使用量增加还是内存泄漏造成的。
是否有任何替代方法setInterval(以预定义的时间间隔执行重复的任务),同时使用更少的计算机资源?
以下代码摘自Jest源代码:
function _path() {
const data = _interopRequireDefault(require('path'));
_path = function _path() {
return data;
};
return data;
}
Run Code Online (Sandbox Code Playgroud)
为什么在中间有命名函数表达式?该模式如何以及何时使用?