Skip to content

Instantly share code, notes, and snippets.

@reyramos
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save reyramos/47fa309a826c1bb9f875 to your computer and use it in GitHub Desktop.

Select an option

Save reyramos/47fa309a826c1bb9f875 to your computer and use it in GitHub Desktop.
angular Controller example calling webworker
'use strict';
/**
* # Application
*
* Core Application controller that includes functions used before we kickStart the Application
* The functions store within this files live outside of the ngView and are used as global function
*/
angular.module('app').controller(
'webworkerController', [
'$rootScope',
'$scope',
'$timeout',
'$log',
'$window',
'Worker',
function ($rootScope, $scope, $timeout, $log, $window, Worker)
{
var worker = new Worker({
url: 'js/workers/doWork2.js' //set the path of the webworker
})
worker.then(function (worker) {
$scope.sayHI = function () {
worker.postMessage({
'cmd': 'start',
'msg': 'Hi'
});
}
$scope.stop = function () {
worker.postMessage({
'cmd': 'stop',
'msg': 'Bye'
});
worker.end();
}
$scope.unknownCmd = function () {
worker.postMessage({
'cmd': 'foobard',
'msg': '???'
});
}
worker.onmessage = function (e) {
document.getElementById('result').textContent = e.data;
}
worker.start()
}, function () {
console.info("ERROR")
})
}
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment