1
let myPos = 0
let load = new Riload({
    el: '#loadMyArticle',
    url: '../users/myArticle',
    data: 'myPos='+myPos,
    sukses: () => {
        myPos = myPos + 1
        console.log(myPos)
    }
})

I have myPos variable where the default value is 0. If load function successfully run, myPos will increased + 1 in every callback. My problem, if I run console.log(myPos) in sukses callback, it can increase. But, myPos in "data" property still valued 0. How to increase this variable on data property?

  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – ponury-kostek Jan 14 '19 at 11:08
  • 2
    `sukses` should be `success` – Ayaz Ali Shah Jan 14 '19 at 11:08
  • `data` has to be a function to accomplish such, otherwise it will concatenate the value. Regardless it will be updated or not, it will **always** be 0, since it's **declared** as a concatenation. – briosheje Jan 14 '19 at 11:09
  • Try to use `this.myPos` – Dmitry Myroshnychenko Jan 14 '19 at 11:10
  • 3
    @Osakr myPos is in a higher scope, so let will work just fine. – Mirakurun Jan 14 '19 at 11:12
  • @Osakr of course it will. http://jsfiddle.net/4fczuxrw/ . I'm surprised no one is guessing that whatever is said above still won't work, since it's just a concatenation, hence it will always result in a **static** string. That's impressive. – briosheje Jan 14 '19 at 11:15
  • My bad so :> haha – Osakr Jan 14 '19 at 11:17
  • @briosheje i could have sworn I saw a comment that gave that answer, probably got removed meantime – Dellirium Jan 14 '19 at 11:17

2 Answers2

1

let myPos = 0
let load = new Riload({
  el: '#loadMyArticle',
  url: '../users/myArticle',
  data: '',
  sukses() {
    this.incrementPos()
  },
  incrementPos() {
    myPos += 1
    this.setData()
  },
  setData() {
    this.data = `myPostTracker = ${myPos}`
  }
})

By accessing the data prop via the setData function you can set the property however you like. I have also added an incrementPos function to the Object so it can be called independently of the success (or sukses) lifecycle callback.

I hope this helps - Originally when you call sukses it was indeed incrementing the myPos variable but the data prop was not being re-set so the myPos variable that you were referencing in your string concatenation was not changing/updating.

let initPos = 0

const obj = {
  myPos: initPos,
  data: '',
  success() {
    this.incrementPos()
  },
  incrementPos() {
    initPos += 1
    this.myPos = initPos
    this.setData()
  },
  setData() {
    this.data = `myPostTracker = ${this.myPos}`
  }
}


obj.success()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)

This approach can be done without a myPos property on the obj Object.

let myPos = 0

const obj = {
  data: '',
  success() {
    this.incrementPos()
  },
  incrementPos() {
    myPos += 1
    this.setData()
  },
  setData() {
    this.data = `myPostTracker = ${myPos}`
  }
}


obj.success()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)

obj.incrementPos()
console.log(obj.data)
Francis Leigh
  • 1,870
  • 10
  • 25
0

this is merely an example of concept. access your newly created object during the "sukses" callback, and update the "data" field from there

let myPos = 0 
let load = new Riload({
    el: '#loadMyArticle',
    url: '../users/myArticle',
    data: 'myPos='+myPos,
    sukses: () => {
        myPos = myPos + 1
        load.data = 'myPos='+myPos
        console.log(myPos)
    } })
Arel Sapir
  • 136
  • 5