Passing a pointer/reference to a variable as parameter

后端 未结 5 1751
情话喂你
情话喂你 2021-01-24 17:40

I know this question has been asked multiple times (yes, I did some research) but I can\'t see to find a solution that fits my needs.

What I have done so far:

5条回答
  •  失恋的感觉
    2021-01-24 18:20

    Try the following example :

    'use strict';
    
    var observable = 0;
    
    function incObservable() {
    	++observable;
    	console.log('incObservable observable: '+observable);
    }
    function observe() {
    	console.log('observe observable: '+observable);
    }
    
    var observer = setInterval(observe, 100);
    
    setTimeout(function() {
    	incObservable();
    	setTimeout(function() {
    		incObservable();
    		setTimeout(function() {
    			incObservable();
    		}, 300);
    	}, 300);
    }, 300);
    
    setTimeout(function() {
    	// Stop obsever
    	clearInterval(observer);
    }, 1000);
    
    // observe observable: 0
    // observe observable: 0
    // incObservable observable: 1
    // observe observable: 1
    // observe observable: 1
    // observe observable: 1
    // incObservable observable: 2
    // observe observable: 2
    // observe observable: 2
    // observe observable: 2
    // incObservable observable: 3
    // observe observable: 3

提交回复
热议问题