尝试在 javascript 中的一段时间后将布尔值从 false 更改为 true
Trying to change a boolean value from false to true after a period of time in javascript
我的网站上有一个人气计数器我正在使用 sails 框架和 node.js,我想使用 cron 库安排一个时间,所以当人气在预定时间(例如一周)后 > 15 ) 它将存档从 false 更改为 true:
这是人气计数器:
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id') || '';
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);
}
}
我的问题是如何在 javascript 中编写代码?下面我粗略的猜了一下我会写什么:
var cron = require('node-cron');
x = popularity
if x < 15:
return cron.schedule('* * * * *' , () => { // secs, mins, hours, DOM, month, DOW
change archived from false - true
});
流行度是根据link在项目前端被点击的次数计算的:
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id',) || '',;
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);
Cron-job 肯定会起作用,但是如果 - 例如:任何东西的受欢迎程度在星期二超过 15,并且您愿意在每个星期五 运行(cron 作业)它。在这种情况下,您将存档延迟到本周晚些时候设置,而不是应该在星期二超过标记 15 时完成。
因此,当您在代码中设置流行度时,请检查它是否超过阈值。如果确实如此,只需将 archived
设置为 true
。您可能不需要为此使用 cron 作业。
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id',) || '',;
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
if (currentPopularity > 15) {
//set the archived state
}
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);
我的网站上有一个人气计数器我正在使用 sails 框架和 node.js,我想使用 cron 库安排一个时间,所以当人气在预定时间(例如一周)后 > 15 ) 它将存档从 false 更改为 true:
这是人气计数器:
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id') || '';
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);
}
}
我的问题是如何在 javascript 中编写代码?下面我粗略的猜了一下我会写什么:
var cron = require('node-cron');
x = popularity
if x < 15:
return cron.schedule('* * * * *' , () => { // secs, mins, hours, DOM, month, DOW
change archived from false - true
});
流行度是根据link在项目前端被点击的次数计算的:
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id',) || '',;
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);
Cron-job 肯定会起作用,但是如果 - 例如:任何东西的受欢迎程度在星期二超过 15,并且您愿意在每个星期五 运行(cron 作业)它。在这种情况下,您将存档延迟到本周晚些时候设置,而不是应该在星期二超过标记 15 时完成。
因此,当您在代码中设置流行度时,请检查它是否超过阈值。如果确实如此,只需将 archived
设置为 true
。您可能不需要为此使用 cron 作业。
const handlePopularityCount = async (event) => {
const bookmarkId = event.target.getAttribute('data-id',) || '',;
if (bookmarkId.length) {
try {
const bookmark = await get(`${URIS.BOOKMARKS}${bookmarkId}`);
const currentPopularity = bookmark.data.popularity || 0;
if (currentPopularity > 15) {
//set the archived state
}
await patch(`${URIS.BOOKMARKS}${bookmarkId}`, {
popularity: currentPopularity + 1,
});
window.open(bookmark.data.url, '_blank');
} catch (error) {
console.log(error);