Skip to content

Instantly share code, notes, and snippets.

@punkbit
Last active February 28, 2017 11:12
Show Gist options
  • Save punkbit/e0843f6ce54d57aa61b52223fb68fcde to your computer and use it in GitHub Desktop.
Save punkbit/e0843f6ce54d57aa61b52223fb68fcde to your computer and use it in GitHub Desktop.

Revisions

  1. punkbit revised this gist Feb 28, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions timed-recursive-calls-with-timer.babel.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    let global = {
    stop: false
    }
    const timeCaller = (params) => {
    const timedCaller = (params) => {
    params.debug && console.log('[DEBUG ' + params.name + '] fn call')
    // Test condition, if true run the callback
    if (params.condition.test()) {
    @@ -24,7 +24,7 @@ const timeCaller = (params) => {
    params.setCallback()
    }
    params.debug && console.log('[DEBUG ' + params.name + '] should recall / recursive')
    timeCaller(params)
    timedCaller(params)
    }, params.time.retryAfterMs)
    } else {
    params.debug && console.log('[DEBUG ' + params.name + '] timeout exceeded')
    @@ -35,7 +35,7 @@ const timeCaller = (params) => {
    }
    }

    timeCaller({
    timedCaller({
    name: 'fnFoobar',
    time: {
    start: new Date().getTime(),
  2. punkbit revised this gist Feb 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion timed-recursive-calls-with-timer.babel.js
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ const timeCaller = (params) => {
    }
    }

    caller({
    timeCaller({
    name: 'fnFoobar',
    time: {
    start: new Date().getTime(),
  3. punkbit revised this gist Feb 28, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions timed-recursive-calls-with-timer.babel.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    let global = {
    stop: false
    }
    const caller = (params) => {
    const timeCaller = (params) => {
    params.debug && console.log('[DEBUG ' + params.name + '] fn call')
    // Test condition, if true run the callback
    if (params.condition.test()) {
    @@ -24,7 +24,7 @@ const caller = (params) => {
    params.setCallback()
    }
    params.debug && console.log('[DEBUG ' + params.name + '] should recall / recursive')
    caller(params)
    timeCaller(params)
    }, params.time.retryAfterMs)
    } else {
    params.debug && console.log('[DEBUG ' + params.name + '] timeout exceeded')
  4. punkbit renamed this gist Feb 28, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. punkbit created this gist Feb 28, 2017.
    62 changes: 62 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    let global = {
    stop: false
    }
    const caller = (params) => {
    params.debug && console.log('[DEBUG ' + params.name + '] fn call')
    // Test condition, if true run the callback
    if (params.condition.test()) {
    params.debug && console.log('[DEBUG ' + params.name + '] condition test passed!')
    if (typeof params.condition.callback === 'function') {
    params.debug && console.log('[DEBUG ' + params.name + '] callback is fn, should call!')
    return params.condition.callback()
    }
    }
    // Keep trying as long it does not exceed max timeout
    params.time.end = new Date().getTime()
    params.time.totalMs = params.time.end - params.time.start
    params.debug && console.log('[DEBUG ' + params.name + '] update params.time: ', params.time)
    if (params.time.totalMs < params.time.maxMs) {
    params.debug && console.log('[DEBUG ' + params.name + '] did not exceed total timeout')
    params.timeout = setTimeout(() => {
    clearTimeout(params.timeout)
    if (typeof params.stepCallback === 'function') {
    params.debug && console.log('[DEBUG ' + params.name + '] has a stepCallback and should call')
    params.setCallback()
    }
    params.debug && console.log('[DEBUG ' + params.name + '] should recall / recursive')
    caller(params)
    }, params.time.retryAfterMs)
    } else {
    params.debug && console.log('[DEBUG ' + params.name + '] timeout exceeded')
    if (typeof params.time.exceedMaxTimeCallback === 'function') {
    params.debug && console.log('[DEBUG ' + params.name + '] has exceedMaxTimeCallback and should call')
    params.time.exceedMaxTimeCallback()
    }
    }
    }

    caller({
    name: 'fnFoobar',
    time: {
    start: new Date().getTime(),
    end: 0,
    totalMs: 0,
    maxMs:10000,
    retryAfterMs: 1000,
    exceedMaxTimeCallback: () => {
    console.log('The caller exceedMaxTimeout!')
    }
    },
    condition: {
    test: () => (global && global.stop),
    callback: () => {
    console.log('Hello world!')
    }
    },
    debug: true
    })
    console.log('global.stop: ', global.stop)
    setTimeout(() => {
    global.stop = true
    console.log('global.stop: ', global.stop)
    }, 5000)