是否可以用来增加ecosia.com主页的种植数量?
const TotalTreecountComponent = View.extend({
initialize(data) {
const model = new TotalTreecountModel(data);
const view = new TotalTreecountView({
model,
el: '.js-total-tree-count-wrapper'
});
this.setElement(view.render().el);
this.timeout = setTimeout(() => {
model.increment();
}, 2000);
this.interval = setInterval(() => {
model.increment();
}, data.secondsToPlantTree * 1000);
},
remove() {
View.prototype.remove.call(this);
window.clearInterval(this.interval);
window.clearTimeout(this.timeout);
}
});
我是js的初学者,所以很酷:D
答案 0 :(得分:0)
setTimeout()
仅在特定时间段后运行一次(此处为2秒(2000毫秒))。
setInterval()
将永远循环,直到window.clearInterval()
此处的代码超时将延迟2秒,然后调用model.increment();
setInterval的代码将重复您在data.secondsToPlantTree
REF:
窗口setTimeout()方法
https://www.w3schools.com/jsref/met_win_settimeout.asp
Window setInterval()方法