我正在尝试使用setInterval
在函数中每1000毫秒更新一次变量。它不起作用,我知道这是一个范围问题。
我可以找到一个适合我的解决方案-在该功能之外的控制台日志记录不会在预定的时间间隔内发生。
您能指出我做错了什么吗?非常感谢!
const interval = 1000;
setInterval(function({
myFunc(`${duration._data.minutes}:${duration._data.seconds}`)},
interval);
function myFunc(vari) {
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
countdown = vari;
}
console.log(countdown);
答案 0 :(得分:0)
您的console.log(倒数)必须位于函数myFunc中,然后console.log将显示每个间隔!
在使用变量
// Action from the ApiController
[HttpGet]
[AddPaginationHeader]
public async Task<IActionResult> Get([FromQuery]PagingModel pagingModel,
[FromHeader(Name = "Accept")]string mediaType) {
var pagedCollection = repository.GetPage(pagingModel);
var shapedCollection = ShapeCollectionOfData(pagedCollection);
return Ok(shapedCollection);
}
// AddPaginationHeader Implementation (Result Filter)
public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) {
var result = context.Result as ObjectResult;
if (result?.Value != null && result?.StatusCode >= 200 &&
result?.StatusCode < 300) {
// Trying to get the pagingModel (but getting a ParameterDescriptor type)
var pagingModel = context.ActionDescriptor.Parameters.Where(t=>t.Name.Equals("pagingModel")).FirstOrDefault();
//Getting the media type
string mediaType = context.HttpContext.Request.Headers["Accept"]
// Getting the query string itself
string queryString = context.HttpContext.Request.QueryString.ToString();
//Implementation of the actual logic that needs the paging model
// ...
next();
}
return Task.CompletedTask;
}
之前,应先创建它,因为如果将持续时间创建为const,则将无法正常工作!
请注意控制台错误!