0

I'm very new to JavaScript. I have the code below that need to check for an image (in this case google logo). if it find the logo, it needs to update "status1" to true, otherwise false. For some reason (likely very obvious to someone that knows what they're doing) this doesn't update and just stays at "false".

Any help with this will be greatly appreciated.

Thanks

function checkImage (src, good, bad) {
    var img = new Image();
    img.onload = good; 
    img.onerror = bad;
    img. src = src;

}


checkImage( "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", function(){ window.status1 = true; }, function(){ window.status1 = false; } );
checkImage( "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", function(){ window.status2 = true; }, function(){ window.status2 = false; } );


var store = new Vuex.Store({
    state: {
        // initial state
        servers: [
        { name: 'test', status: window.status1, adr: 'http://google.com' }]},


Cid
  • 14,968
  • 4
  • 30
  • 45
  • Is some of the code missing? – Cereal Feb 19 '20 at 13:05
  • Those `onload` callbacks will be executed *sometime later* when the image has done loading. You're not awaiting that result. – deceze Feb 19 '20 at 13:07
  • 2
    If you're indeed "very new to JavaScript", then starting with Vue and VueX might be counterproductive. Learn vanilla JS first, then add frameworks and libraries to your tool belt. – mbojko Feb 19 '20 at 13:12

0 Answers0