Skip to content

Commit

Permalink
fix: change timeout calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Dec 20, 2020
1 parent 01a5f4d commit 8aa9c2d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/checkOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ export default function checkOptions(data, parameterizedFunction, options) {
);
}

let checkTime;
let checkTimeout;
if (timeout !== undefined) {
if (typeof timeout !== 'number') {
throw new Error('timeout should be a number');
}
checkTime = (start) => Date.now() - start >= timeout * 1000;
let endTime = Date.now() + timeout * 1000;
checkTimeout = () => Date.now() > endTime;
} else {
checkTime = () => false;
checkTimeout = () => false;
}

let weightSquare = new Array(data.x.length);
Expand All @@ -95,7 +96,7 @@ export default function checkOptions(data, parameterizedFunction, options) {
}

return {
checkTime,
checkTimeout,
minValues,
maxValues,
parameters,
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function levenbergMarquardt(
options = {},
) {
let {
checkTime,
checkTimeout,
minValues,
maxValues,
parameters,
Expand All @@ -44,8 +44,6 @@ export default function levenbergMarquardt(
improvementThreshold,
} = checkOptions(data, parameterizedFunction, options);

const startTime = Date.now();

let error = errorCalculation(
data,
parameters,
Expand Down Expand Up @@ -99,7 +97,7 @@ export default function levenbergMarquardt(
damping = Math.min(damping * dampingStepUp, 1e7);
}

if (checkTime(startTime)) {
if (checkTimeout()) {
throw new Error(
`The execution time is over to ${options.timeout} seconds`,
);
Expand Down

0 comments on commit 8aa9c2d

Please sign in to comment.