Revision b1e6a2de ui/static/snf/js/sync.js
b/ui/static/snf/js/sync.js | ||
---|---|---|
343 | 343 |
this.call_on_start = options.call_on_start || true; |
344 | 344 |
|
345 | 345 |
this.running = false; |
346 |
this.last_call = false; |
|
346 | 347 |
|
347 | 348 |
// wrapper |
348 | 349 |
function _cb() { |
... | ... | |
355 | 356 |
this.setInterval() |
356 | 357 |
} |
357 | 358 |
this.cb(); |
359 |
this.last_call = new Date; |
|
358 | 360 |
}; |
359 | 361 |
|
360 | 362 |
_cb = _.bind(_cb, this); |
... | ... | |
368 | 370 |
this.setInterval = function() { |
369 | 371 |
this.trigger("clear"); |
370 | 372 |
window.clearInterval(this.interval); |
373 |
|
|
371 | 374 |
this.interval = window.setInterval(_cb, this.timeout); |
372 | 375 |
this.running = true; |
373 |
if (this.call_on_start) { |
|
376 |
|
|
377 |
var call = this.call_on_start; |
|
378 |
|
|
379 |
if (this.last_call) { |
|
380 |
var next_call = (this.timeout - ((new Date) - this.last_call)); |
|
381 |
if (next_call < this.timeout/2) { |
|
382 |
call = true; |
|
383 |
} else { |
|
384 |
call = false; |
|
385 |
} |
|
386 |
} |
|
387 |
|
|
388 |
if (call) { |
|
374 | 389 |
_cb(); |
375 | 390 |
} |
391 |
return this; |
|
376 | 392 |
} |
377 | 393 |
|
378 | 394 |
this.start = function (call_on_start) { |
395 |
if (this.running) { this.stop() }; |
|
379 | 396 |
this.call_on_start = call_on_start == undefined ? this.call_on_start : call_on_start; |
380 | 397 |
this.setInterval(); |
398 |
return this; |
|
381 | 399 |
} |
382 | 400 |
|
383 | 401 |
this.stop = function() { |
384 | 402 |
this.trigger("clear"); |
385 | 403 |
window.clearInterval(this.interval); |
386 | 404 |
this.running = false; |
405 |
return this; |
|
387 | 406 |
} |
388 | 407 |
} |
389 | 408 |
|
Also available in: Unified diff